c++宏给出了C4430错误:缺少类型说明符

C++ macro gives C4430 error: Missing type specifier

本文关键字:类型 说明符 错误 C4430 c++      更新时间:2023-10-16

我遵循Google的c++风格指南,该指南建议:对于类,应该添加宏

#define DISALLOW_COPY_AND_ASSIGN(TypeName) 
  TypeName(const TypeName&);               
  void operator=(const TypeName&) 
class MyClass {
...
DISALLOW_COPY_AND_ASSIGN(MyClass);
};
#undef DISALLOW_COPY_AND_ASSIGN

我将这个宏添加到项目中的许多类中。当我编译时,我得到错误:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

任何想法?

...部分错误。此外,最好将您的复制和传递操作符delete,而不是仅仅隐藏它们

class Foo
{
public:
    Foo(Foo&) = delete;
    Foo& operator=(const Foo&) = delete;
}

注意delete是c++11特性

我相信你在这里少了一个分号:

void operator=(const TypeName&) 

既然这个被否决了,我就证明给你看。

这是你的原始代码(与...删除和添加一些其他的东西,使其实际编译你的类),未能编译:

#define DISALLOW_COPY_AND_ASSIGN(TypeName) 
  TypeName(const TypeName&);               
  void operator=(const TypeName&) 
class MyClass {
public: MyClass()
:
  mN (42)
{
}
private:
  int mN; 
DISALLOW_COPY_AND_ASSIGN(MyClass)
  long mL; 
};
#undef DISALLOW_COPY_AND_ASSIGN
int main()
{
  MyClass c;

}

在c++ 4.8下,编译器报错:

jdibling@hurricane /home/jdibling/dev/hacks $ g++ main.cpp 
main.cpp:3:33: error: expected ‘;’ at end of member declaration
   void operator=(const TypeName&) 
                                 ^
main.cpp:14:1: note: in expansion of macro ‘DISALLOW_COPY_AND_ASSIGN’
 DISALLOW_COPY_AND_ASSIGN(MyClass)
 ^

如果我们编辑宏定义以包含分号:

#define DISALLOW_COPY_AND_ASSIGN(TypeName) 
  TypeName(const TypeName&);               
  void operator=(const TypeName&);

它编译干净:

jdibling@hurricane /home/jdibling/dev/hacks $ g++ main.cpp 
jdibling@hurricane /home/jdibling/dev/hacks $