我已经采取了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;
}
我在做什么错?我没有指定返回类型。或者我不知何故?
请*初始化列表*读了起来:http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=172 – 2009-12-15 17:49:32
好吧,我会的。 – Alex 2009-12-15 17:51:00
IsCountable可以简化为'return count == 0'。 顺便说一句,为什么你有一个'isCounter'成员,如果它从未被读取(使用)? – 2009-12-15 19:14:03