views:

138

answers:

2

Hi there, I've taken 2 OOP C# classes, but now our professor is switching over to c++. So, to get used to c++, I wrote this very simple program, but I keep getting this error:

error C2533: 'Counter::{ctor}' : constructors not allowed a return type

I'm confused, because I believe I have coded my default constructor right.

Here's my code for the simple counter class:

class Counter
{
private:
int count;
bool isCounted;

public:
Counter();
bool IsCountable();
void IncrementCount();
void DecrementCount();
int GetCount();
}

Counter::Counter()
{
count = 0;
isCounted = false;
}

bool Counter::IsCountable()
{
if (count == 0)
 return false;
else
 return true;
}

void Counter::IncrementCount()
{
count++;
isCounted = true;
}

void Counter::DecrementCount()
{
count--;
isCounted = true;
}

int Counter::GetCount()
{
return count;
}

What am I doing wrong? I'm not specifying a return type. Or am I somehow?

+14  A: 

You forgot the semi-colon at the end of your class definition. Without the semi-colon, the compiler thinks the class you just defined is the return type for the constructor following it in the source file. This is a common C++ error to make, memorize the solution, you'll need it again.

class Counter
{
private:
int count;
bool isCounted;

public:
Counter();
bool IsCountable();
void IncrementCount();
void DecrementCount();
int GetCount();
};
FigBug
wow. That was it. In c++ you need semicolons after your class definitions?
Alex
This is where GCC shines a bit - you get the diagnostic "note: (perhaps a semicolon is missing after the definition of 'Counter')"
anon
@Alex Yes - just like you need them after struct definitions in C.
anon
Ok, thanks. :) I'll remember that.
Alex
Between the missing semicolon end the missing namespace end... sometimes you really are in for a world of pain... and the fact that it crosses files (with the include mechanism) isn't really charming either :/
Matthieu M.
@Neil: In every case? MSVC does the same in some cases, but it obviously depends on the context. Sometimes, it's not able to guess that this was a possible explanation for the error.
jalf
+1  A: 

You need to end your class declaration with a semicolon.

class Counter
{
private:
int count;
bool isCounted;

public:
Counter();
bool IsCountable();
void IncrementCount();
void DecrementCount();
int GetCount();
} ;
William Bell