0

I'm trying to following to application in OOP C but I get errors in my new.h file.

I am getting a bunch of "unexpected" errors. Just about everything after the first 'void' is exepected. Any ideas?

#ifndef NEW_H
#define NEW_H

#ifdef  __cplusplus
extern "C" {
#endif

void * new (const void * type);
void delete (void * item);  


#ifdef  __cplusplus
}
#endif



#endif  /* NEW_H */

enter image description here

4

4 回答 4

2

这似乎是少数在 C++ 下无效的 C 程序之一:标识符在 Cnewdelete完全有效,但在 C++ 下被保留。

于 2012-10-25T05:14:32.513 回答
1

new 和 delete 是 C++ 中的关键字,而不是合法的函数名。

即使您认为您正在编译 C,也请测试它是否可以修复它。如果没有,那可能是经典的 C/C++ 神秘错误:问题出在前面的 include 中,可能是缺少分号什么的。

于 2012-10-25T05:14:20.090 回答
1

好吧,您正在编译为 C,但newanddelete运算符仅在 C++ 中受支持。

new此外,和的签名delete是:

void* operator new(size_t n);
void operator delete(void* p);

您还可以为特定类重载这些运算符(通过在类中声明它们)。

于 2012-10-25T05:12:28.487 回答
0

如果你想模拟 in 的功能,new那么CC++实际上得到了错误的参数new。应该是const unsigned int或者const size_t 因为 new 在 C++ 中被用作

int *p;
p=new (sizeof(int));

但是这里的类型不匹配无法转换intconst void *

因为delete这很好,因为delete作为pointer论据

于 2012-10-25T05:45:07.363 回答