我想知道爲什麼下面的代碼只返回「測試」四次而不是五次?關於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