我需要为类Timer重载增量。我班的成员只有几分钟和几秒钟。
#include <iostream>
#include <conio.h>
using namespace std;
class Timer
{
private:
int minutes;
int seconds;
public:
Time(){
minutes = 0;
seconds = 0;
}
Time(int m, int s){
minutes = m;
seconds = s;
}
void displayTime()
{
cout << "M: " << hours << " S:" << minutes <<endl;
}
Time operator++ ()
{
++seconds;
if(seconds >= 60)
{
++minutes;
seconds -= 60;
}
return Time(minutes, seconds);
}
Time operator++( int )
{
Time T(minutes, seconds);
++seconds;
if(seconds >= 60)
{
++minutes;
seconds -= 60;
}
return T;
}
};
int main()
{
Time T1(18, 23), T2(19,12);
++T1;
T1.displayTime();
++T1;
T1.displayTime();
T2++;
T2.displayTime();
T2++;
T2.displayTime();
_getch()
}
当我调试时,它说
编译器:默认编译器构建Makefile:“ C:\ Dev-Cpp \ Makefile.win”正在执行make ... make.exe -f“ C:\ Dev-Cpp \ Makefile.win”所有g ++。exe -c main.cpp -o main.o -I“ C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include” -I“ C:/ Dev-Cpp / include / c ++ / 3.4.2 / backward” -I“ C:/ Dev-Cpp / include / c ++ / 3.4.2 / mingw32“ -I” C:/ Dev-Cpp / include / c ++ / 3.4.2“ -I” C:/ Dev-Cpp / include“
main.cpp:13: error: ISO C++ forbids declaration of `Time' with no type main.cpp:18: error: ISO C++ forbids declaration of `Time' with no type main.cpp:29: error: ISO C++ forbids declaration of `Time' with no type main.cpp:29: error: expected `;' before "operator" main.cpp:40: error: expected `;' before "Time" main.cpp:40: error: ISO C++ forbids declaration of `Time' with no type main.cpp:40: error: expected `;' before "operator" main.cpp:54: error: expected `;' before '}' token main.cpp: In member function `void Timer::displayTime()': main.cpp:26: error: `hours' undeclared (first use this function) main.cpp:26: error: (Each undeclared identifier is reported only once for each function it appears in.) main.cpp: At global scope: main.cpp:56: error: new types may not be defined in a return type main.cpp:56: error: extraneous `int' ignored main.cpp:56: error: `main' must return `int' main.cpp: In function `int main(...)': main.cpp:57: error: `Time' undeclared (first use this function) main.cpp:57: error: expected `;' before "T1" main.cpp:59: error: `T1' undeclared (first use this function) main.cpp:64: error: `T2' undeclared (first use this function) main.cpp:69: error: expected `;' before '}' token make.exe: *** [main.o] Error 1 Execution terminated
对象应该是计时器类型而不是时间类型。尝试匹配类名和构造函数名。
在displayTime方法中未定义小时成员。
void displayTime()
{
cout << "M: " << minutes << " S:" << seconds <<endl;
}
请参考以下代码http://ideone.com/m50w2r
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句