在此范围内未声明类功能
Class Function Not declared in this scope?
我在添加了类的功能中遇到了一些错误,这是我的构建日志:
-------------- Build: Debug in Lab1 (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -Wall -g -c C:UsersDekkillerDocumentsclockType.cpp -o
objDebugclockType.o
C:UsersDekkillerDocumentsclockType.cpp: In function 'clockType operator+(const
clockType&, const clockType&)':
C:UsersDekkillerDocumentsclockType.cpp:51:21: error: 'incrementhr' was not declared
in this scope
incrementhr();
^
C:UsersDekkillerDocumentsclockType.cpp:54:22: error: 'incrementmin' was not declared
in this scope
incrementmin();
^
C:UsersDekkillerDocumentsclockType.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();
}
}
任何帮助都受到赞赏,我已经在此程序上工作了一段时间,但似乎无法使它完全工作,也许我认为我正在错误地实施增量功能或我如何修复此增量。
clockType operator+(const clockType& one, const clockType& two)
{
clockType three;
three.hrs = one.hrs + two.hrs;
if(three.hrs>23)
incrementhr();
朋友功能不是与之交友的类型的成员。在这种情况下,操作员是一个免费功能。成员函数incrementhr
是成员函数,并调用您需要对象。您可能的意思是: three.incrementhr()
?
相关文章:
- #ifdef和未声明的标识符
- 未声明的标识符编译暗黑破坏神 2 程序"muleview"
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- Google protobuf 时间戳未声明标识符,在 Windows 上具有C++
- 在气泡排序程序中未声明错误功能
- 我在范围内未声明的错误类有问题
- 使用未声明的标识符"sqrt"?
- 我包含了<winuser.h>,但编译器在MonitorFromWindow上抛出"未声明"
- 对类 (C++) 中的私有变量使用未声明的标识符
- 我的代码中有错误,未声明的标识符
- 未定义的标识符/未声明
- 错误:在 C++ 'std'使用未声明的标识符
- 在编译器之前检测未声明的标识符
- 使用未声明的标识符'temp'
- 使用未声明的标识符"yylex"和"yyin"?
- 使用"class"关键字,后跟未声明的标识符
- 未声明的标识符错误,但变量定义正确 (?)
- 功能未在范围(GLFW,OpenGL,Slackware)中声明
- 在此范围内未声明类功能
- GMP - 某些功能未声明,其他功能是?C++/分钟