gpt4 book ai didi

c++ - 在类计时器中重载前缀和后缀增量。调试时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:26 25 4
gpt4 key购买 nike

我需要为类 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()
}

当我调试时,它说

Compiler: Default compiler Building Makefile: "C:\Dev-Cpp\Makefile.win" Executing make... make.exe -f "C:\Dev-Cpp\Makefile.win" all 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

最佳答案

对象的类型应该是 Timer 而不是 Time。尝试匹配类名和构造函数名。

Hour 成员未在 displayTime 方法中定义。

void displayTime()
{
cout << "M: " << minutes << " S:" << seconds <<endl;
}

请引用以下代码 http://ideone.com/m50w2r

关于c++ - 在类计时器中重载前缀和后缀增量。调试时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23723892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com