"myClock"未在此范围错误中声明

‘myClock’ was not declared in this scope error

本文关键字:quot 错误 声明 范围 myClock      更新时间:2023-10-16

实现文件中发生错误。我正在尝试使用带有差分函数的 myClock 对象并在其中传递您的时钟,从而允许我减去彼此经过的两个时间并找到它们之间的差异。

类似于 equalTime 函数使用 myClock 和 yourClock 作为参数并复制时间的工作方式。除了在从 timeElapsed 函数经过这些时间后对其进行主题,然后打印差异。

我继续收到此错误,我不明白它在哪里,我必须声明 myClock。如果很简单,我深表歉意。我目前正在大学完成这些作业,并试图从错误中吸取教训。下面也是确切的错误。

clockTypeImp.cpp: In member function ‘void clockType::difference(clockType&)’:
clockTypeImp.cpp:43:13: error: ‘myClock’ was not declared in this scope
diff = (myClock.elapsedTime() - o
^
bash: line 2: ./a.out: No such file or directory

时钟类型.h

class clockType
{
public: 
void setTime(int hours, int minutes, int seconds);
void setHours(int hours);
void setMinutes(int minutes);
void setSeconds(int seconds);
long elapsedTime();
long remainingTime();
void difference(clockType& otherClock);
void getTime(int& hours, int& minutes, int& seconds) const;
int getHours() const
{
return hr;
}
int getMinutes() const
{
return min;
}
int getSeconds() const
{
return sec;
}
void printTime() const;
void incrementSeconds();
void incrementMinutes();
void incrementHours();
bool equalTime(const clockType& otherClock) const;
clockType(int hours, int minutes, int seconds);
clockType();
private:
int hr;  //variable to store the hours
int min; //variable to store the minutes
int sec; //variable to store the seconds
};

时钟类型.cpp

//Implementation File for the class clockType
#include <iostream>
#include <cmath>
#include "clockType.h"
using namespace std; 
void clockType::setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else 
hr = 0;
if (0 <= minutes && minutes < 60)
min = minutes;
else 
min = 0;
if (0 <= seconds && seconds < 60)
sec = seconds;
else 
sec = 0;
}
long clockType::elapsedTime()
{
return (hr * 3600 + min * 60 + sec);
}
long clockType::remainingTime()
{
return ((24 * 3600) - (hr * 3600 + min * 60 + sec));
}
void clockType::difference(clockType& otherClock)
{
long diff;
int hrs, mins, secs;
diff = (myClock.elapsedTime() - otherClock.elapsedTime());
if (diff > 0){
hrs = (diff / 3600);
mins = (diff - hrs * 3600) / 60;
secs = (diff - (hrs * 3600 + mins * 60));
cout << "The Clock 2 is far apart from clock 1 by: ";
if (hrs < 10)
cout << "0";
cout << hrs << ":";
if (mins < 10)
cout << "0";
cout << mins << ":";
if (secs < 10)
cout << "0";
cout << secs << endl; 
}
else if (diff < 0)
{
diff = (otherClock.elapsedTime() - myClock.elapsedTime());
hrs = (diff / 3600);
mins = (diff - hrs * 3600) / 60;
secs = (diff - (hrs * 3600 + mins * 60));
cout << "The Clock 1 is far apart from clock 2 by: ";
if (hrs < 10)
cout << "0";
cout << hrs << ":";
if (mins < 10)
cout << "0";
cout << mins << ":";
if (secs < 10)
cout << "0";
cout << secs << endl; 
}
else 
{
cout << "Both the clocks have the same time " << "The difference is 00:00:00" << endl;
}
}
void clockType::setHours(int hours)
{
if (0 <= hours && hours < 24)
hr = hours;
else 
hr = 0;
}
void clockType::setMinutes(int minutes)
{
if (0 <= minutes && minutes < 60)
min = minutes;
else 
min = 0;
}
void clockType::setSeconds(int seconds)
{
if (0 <= seconds && seconds < 60)
sec = seconds;
else 
sec = 0;
}
void clockType::getTime(int& hours, int& minutes, int& seconds) const
{
hours = hr;
minutes = min;
seconds = sec;
}
void clockType::incrementHours()
{
hr++;
if(hr > 23)
hr = 0;
}
void clockType::incrementMinutes()
{
min++;
if (min > 59)
{
min = 0;
incrementHours();
}
}
void clockType::incrementSeconds()
{
sec++;
if (sec > 59)
{
sec = 0;
incrementMinutes();
}
}
void clockType::printTime() const
{
if (hr < 10)
cout << "0";
cout << hr << ":";
if (min < 10)
cout << "0";
cout << min << ":";
if (sec < 10)
cout << "0";
cout << sec;
}
bool clockType::equalTime(const clockType& otherClock) const
{
return (hr == otherClock.hr 
&& min == otherClock.min 
&& sec == otherClock.sec);
}
clockType::clockType(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else 
hr = 0;
if (0 <= minutes && minutes < 60)
min = minutes;
else 
min = 0;
if (0 <= seconds && seconds < 60)
sec = seconds;
else 
sec = 0;
}
clockType::clockType()  //default constructor
{
hr = 0;
min = 0;
sec = 0;
}

主.cpp

//The user program that uses the class clockType
#include <iostream> 
#include "clockType.h"
using namespace std;
int main()
{
clockType myClock;
clockType yourClock;   
int hours;
int minutes;
int seconds;
myClock.setTime(5, 4, 30); 
cout << "myClock: "; 
myClock.printTime();
cout << endl;
cout << "yourClock: "; 
yourClock.printTime();
cout << endl; 
//Set the time of yourClock
yourClock.setTime(5, 45, 16); 
cout << "After setting, yourClock: ";
yourClock.printTime();
cout << endl; 
cout << "Enter the hours, minutes, and "
<< "seconds: ";  
cin >> hours >> minutes >> seconds; 
cout << endl; 
myClock.setHours(hours);
myClock.setMinutes(minutes);
myClock.setSeconds(seconds);
cout << "myClock: "; 
myClock.printTime(); 
cout << endl;  
myClock.incrementSeconds(); 
cout << "After incrementing myClock by " 
<< "one second, myClock: "; 
myClock.printTime(); 
cout << endl; 
//Output the value of hours, minutes, and seconds
//of myClock
cout << "hours = " << myClock.getHours() 
<< ", minutes = " << myClock.getMinutes() 
<< ", seconds = " << myClock.getSeconds() << endl << endl << endl;
cout << "The elapsed time of the day is: " <<
myClock.elapsedTime() << " seconds" << endl;
cout << "The remaining time of the day is: " << myClock.remainingTime() << " seconds" << endl;
myClock.difference(yourClock);
return 0;
}//end main

如果你想要的是获得与当前时钟相对于传递时钟的差异,你可以做的是用diff = (elapsedTime() - otherClock.elapsedTime())替换diff = (myClock.elapsedTime() - otherClock.elapsedTime());

发生的情况是,即使您在主函数中声明 myClock,它也永远不会传递给函数difference()。在我给出的建议中,它的作用是,差分函数将调用它的elapsedTime()函数(相对于 myClock 对象(,然后将其与传递的 ClockType 对象进行比较。