在此范围内未声明类功能

Class Function Not declared in this scope?

本文关键字:功能 未声明 范围内      更新时间:2023-10-16

我在添加了类的功能中遇到了一些错误,这是我的构建日志:

-------------- 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()