Having code:
struct B { int* a; B(int value):a(new int(value)) { } B():a(nullptr){} B(const B&); } B::B(const B& pattern) { }
I'm getting err msg:
'Error 1 error C2533: 'B::{ctor}' : constructors not allowed a return type'
Any idea why?
P.S. I'm using VS 2010RC
A constructor cannot have a return type (not even a void return type). A common source of this error is a missing semicolon between the end of a class definition and the first constructor implementation. The compiler sees the class as a definition of the return type for the constructor function, and generates C2533.
Since constructor can only return the object to class, it's implicitly done by java runtime and we are not supposed to add a return type to it. If we add a return type to a constructor, then it will become a method of the class. This is the way java runtime distinguish between a normal method and a constructor.
1 Answer. A constructor cannot specify any return type, not even void. A constructor cannot be final, static or abstract.
You're missing a semicolon after your struct
definition.
The error is correct, constructors have no return type. Because you're missing a semicolon, that entire struct definition is seen as a return type for a function, as in:
// vvv return type vvv struct { /* stuff */ } foo(void) { }
Add your semicolon:
struct B { int* a; B(int value):a(new int(value)) { } B():a(nullptr){} B(const B&); }; // end class definition // ah, no return type B::B(const B& pattern) { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With