Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of this Constructor error?

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?

like image 275
Alex Avatar asked Nov 17 '25 20:11

Alex


1 Answers

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();
};
like image 132
Roland Rabien Avatar answered Nov 20 '25 10:11

Roland Rabien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!