devotee
devotee

Reputation: 127

C++ macro gives C4430 error: Missing type specifier

I am following Google's C++ style guide which suggests: For classes, one should add the macro

#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

I add this macro to a number of classes in my project. When I compile, I get the error:

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

Any ideas?

Upvotes: 0

Views: 952

Answers (2)

Quest
Quest

Reputation: 2843

An error is in ... part. Also It's better to delete your copy and assing operator not just hiding them

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

Note that delete is c++11 feature

Upvotes: 4

John Dibling
John Dibling

Reputation: 101446

I do believe you're missing a semicolon here:

void operator=(const TypeName&) 

Since this was downvoted, I'll prove it.

Here is your original code (with the ... removed and some other stuff added in to make it actually compile your class), failing to compile:

#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;


}

Under G++4.8, the compiler complains:

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)
 ^

If we edit the macro definition to include a semicolon:

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

It compiles clean:

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

Upvotes: 2

Related Questions