我想知道为什么下面的代码只返回“测试”四次而不是五次?关于Bool在C++中的问题
#include <iostream>
#include <cassert>
using namespace std;
class CountDown
{
public: //Application Programmer Interface
CountDown(int start); // it is set to start
void next(); // subtracts one from it
bool end()const; //
private:
int it;
};
CountDown::CountDown(int start)
{
it = 0;
it = start;
}
void CountDown::next()
{
it = it - 1;
}
bool CountDown::end() const
{
if (it <= 0)
cout << "The countdown is now over" << endl;
}
int main()
{
for(CountDown i = 5 ; ! i.end(); i.next())
std::cerr << "test\n";
}
您是否缺少''end'方法中的'return'? – sje397 2011-05-19 05:57:17
你能发布'end'方法的正确定义吗? – Naveen 2011-05-19 05:57:56
我也有点困惑,'CountDown i = 5'这句话不应该起作用。不应该是'CountDown * i = new CountDown(5)'或者'CountDown i(5)' – 2011-05-19 06:21:46