我在一个类中添加的函数中出现了一些错误,这是我的构建日志:
-------------- Build: Debug in Lab1 (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -Wall -g -c C:\Users\Dekkiller\Documents\clockType.cpp -o
obj\Debug\clockType.o
C:\Users\Dekkiller\Documents\clockType.cpp: In function 'clockType operator+(const
clockType&, const clockType&)':
C:\Users\Dekkiller\Documents\clockType.cpp:51:21: error: 'incrementhr' was not declared
in this scope
incrementhr();
^
C:\Users\Dekkiller\Documents\clockType.cpp:54:22: error: 'incrementmin' was not declared
in this scope
incrementmin();
^
C:\Users\Dekkiller\Documents\clockType.cpp:59:1: warning: control reaches end of non-
void function [-Wreturn-type]
}
^
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 1 warning(s) (0 minute(s), 0 second(s))
这是我的主要cpp代码:
#include <iostream>
#include "clockType.h"
using namespace std;
int main()
{
clockType c1(15, 45, 30), c2(3, 20); // hour, min, sec
cout << "c1 is " << c1; // add whatever to beautify it
cout << "c2 is " << c2;
cout << "c1+c2 is " << c1+c2;
c2 = c1+c1;
cout << "c1+c1 is " << c2;
}
这是我的标题类文件:
#ifndef CLOCKTYPE_H
#define CLOCKTYPE_H
#include <iostream>
class clockType
{
friend std::ostream& operator<<(std::ostream& os, const clockType& out);
friend clockType operator+(const clockType& one, const clockType& two);
public:
clockType();
clockType(int hours, int minutes, int seconds);
clockType(int hours, int minutes);
void setTime(int hours, int minutes, int seconds);
void incrementhr();
void incrementmin();
void incrementsec();
private:
int hrs;
int mins;
int secs;
};
#endif // CLOCKTYPE_H
这是我的cpp类文件:
#include "clockType.h"
#include <iostream>
using namespace std;
clockType::clockType()
{
hrs = 0;
mins = 0;
secs = 0;
}
clockType::clockType(int hours, int minutes, int seconds)
{
setTime(hours, minutes, seconds);
}
clockType::clockType(int hours, int minutes)
{
hrs = hours;
mins = minutes;
secs = 0;
}
void clockType::setTime(int hours, int minutes, int seconds)
{
if(0<=hours && hours<24)
hrs=hours;
else
hrs=0;
if(0<=minutes && minutes<60)
mins=minutes;
else
mins=0;
if(0<=seconds && seconds<60)
secs=seconds;
else
secs=0;
}
ostream& operator<<(ostream& os, const clockType& out)
{
os << "Hour is " << out.hrs << "Minute is " << out.mins << "Seconds is " << out.secs;
return os;
}
clockType operator+(const clockType& one, const clockType& two)
{
clockType three;
three.hrs = one.hrs + two.hrs;
if(three.hrs>23)
incrementhr();
three.mins = one.mins + two.mins;
if(three.mins>59)
incrementmin();
three.secs = one.secs + two.secs;
if(three.secs>59)
return three;
}
void clockType::incrementhr()
{
hrs++;
if(hrs > 23)
hrs=0;
}
void clockType::incrementmin()
{
mins++;
if(mins>59)
{
mins=0;
incrementhr();
}
}
void clockType::incrementsec()
{
secs++;
if(secs>59)
{
secs=0;
incrementmin();
}
}
任何帮助表示赞赏,我已经在这个程序上工作了一段时间,但似乎无法让它完全正常工作,也许我正在实现增量函数不正确我想但我想不出怎么回事或者我如何解决这个增量问题。
答案 0 :(得分:1)
clockType operator+(const clockType& one, const clockType& two)
{
clockType three;
three.hrs = one.hrs + two.hrs;
if(three.hrs>23)
incrementhr();
朋友功能不是与他们成为朋友的类型的成员。在这种情况下,操作员是自由功能。成员函数incrementhr
是一个成员函数,并且调用它需要一个对象。你可能意味着:three.incrementhr()
?