如何摆脱这个Constructor错误?

Ale*_*lex 2 c++ compiler-errors

我已经参加了2个OOP C#课程,但现在我们的教授正在转向c ++.所以,为了习惯c ++,我写了这个非常简单的程序,但我一直收到这个错误:

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

我很困惑,因为我相信我已经将我的默认构造函数编码为正确.

这是我的简单计数器类的代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我没有指定返回类型.或者我是某种程度?

Rol*_*ien 14

您在类定义的末尾忘记了分号.如果没有分号,编译器会认为您刚定义的类是源文件中跟随它的构造函数的返回类型.这是一个常见的C++错误,记住解决方案,你将再次需要它.

class Counter
{
private:
int count;
bool isCounted;

public:
Counter();
bool IsCountable();
void IncrementCount();
void DecrementCount();
int GetCount();
};
Run Code Online (Sandbox Code Playgroud)