Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ctor not allowed return type

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

like image 317
There is nothing we can do Avatar asked Apr 05 '10 16:04

There is nothing we can do


People also ask

Which constructors are not allowed to have a return type?

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.

Why constructor cannot have return type in java?

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.

Which constructor declaration is not valid in C++?

1 Answer. A constructor cannot specify any return type, not even void. A constructor cannot be final, static or abstract.


1 Answers

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) {  } 
like image 179
GManNickG Avatar answered Sep 20 '22 23:09

GManNickG