実装ファイルでエラーが発生しています。myClockオブジェクトをdifference関数で使用し、yourClockをその中に渡して、互いに経過した時間の両方を減算し、それらの違いを見つけようとしています。
パラメータとしてyourClockを指定してmyClockを使用し、時間をコピーするequalTime関数の動作と同様です。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
clockType.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
};
clockTypeImp.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;
}
main.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 = (myClock.elapsedTime() - otherClock.elapsedTime());
とdiff = (elapsedTime() - otherClock.elapsedTime())
メインでmyClockを宣言しても、関数に渡されることはありませんdifference()
。私が行った提案では、差分関数はelapsedTime()
(myClockオブジェクトに関して)その関数を呼び出し、渡されたClockTypeオブジェクトと比較します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加