1
我與時間項目有關的問題,我將在下面完整發布以提供上下文。我所做的大部分都是正確的,但是在加法和減法操作符定義時,我遇到了範圍錯誤。編譯器找不到我的私有成員。 (如果用戶在25小時內進入小時,那麼操作員可以使用它,它會循環回到1)。項目作爲一個整體創建已定義方法的實例。無論如何,第二個錯誤與運營商本身有關。我得到一個錯誤範圍錯誤,不能有一個cv限定符?
做研究告訴我,這個錯誤發生在改變const,在我的情況是時間本身。我看不到我在那裏做了什麼。我希望我很清楚,如果需要可以澄清。 .h文件來自練習本身,除了添加私人成員之外,我無法改變它。
#ifndef TIME_H_
#define TIME_H_
#include <iostream>
#include <string>
/*** Time class** The Time class contains time as
hours:minutes:seconds:milliseconds (AM/PM).*/
class Time {
public:
/** * Constructor with zero values */
Time();
/** * Constructors with arguments */
Time(long long time);
Time(int hours, int minutes, int seconds, int milli);
/** * Deconstructor */
virtual ~Time();
/** * Return time as a long long value representing time in milliseconds */
long long asLong() const;
/** * Provide a string in the format hours:minutes:seconds:milliseconds. * For example 1:45:30:56 PM */
std::string toString() const;
/** * Output the time to an output stream as hours:minutes:seconds:milliseconds AM/PM */
friend std::ostream& operator <<(std::ostream&, const Time&);
// Output a Time to an output stream
/** * Declare ordering relationships */
friend bool operator <(const Time&, const Time&);
friend bool operator >(const Time&, const Time&);
friend bool operator ==(const Time &a, const Time &b);
/** * Declare addition and subtraction */
Time operator +(const Time&) const;
Time operator -(const Time&) const;
private:
int hours;
int minutes;
int seconds;
int millis;
};
#endif /* TIME_H_ */
然後,源自身。
#include "Time.h"
#include <sstream>
#include <string>
using namespace std;
// Defualt Constructor
Time::Time() {
hours = 0;
minutes = 0;
seconds = 0;
millis = 0;
}
// Constructors with arguments
Time::Time(long long timeValue) {
long long tempValue = timeValue;
millis = tempValue % 1000;
tempValue /= 1000;
seconds = tempValue % 60;
tempValue /= 60;
minutes = tempValue % 60;
tempValue /= 60;
hours = tempValue;
}
Time::Time(int hours, int minutes, int seconds, int millis) {
this->hours = hours;
this ->minutes = minutes;
this -> seconds = seconds;
this -> millis = millis;
}
// Destructor
Time::~Time() {
}
// Return time in term of milliseconds.
long long Time::asLong() const {
long long timeValue = (long long) hours;
timeValue = (timeValue * 60) + minutes;
timeValue = (timeValue * 60) + seconds;
timeValue = (timeValue * 1000) + millis;
return timeValue;
}
// Formatting
std::string Time::toString() const {
ostringstream v1;
string ph;
if (hours <= 12)
ph = "am";
else
ph = "pm";
v1 << hours % 12 << ":" << minutes << ":" << seconds << ":" << millis << ph;
return v1.str();
}
// Time to Output Stream
ostream& operator <<(ostream& a, const Time& b)
{
return a << b.toString();
}
// Ordering Relationships
bool operator <(const Time&t1, const Time&t2)
{
return t1.asLong() < t2.asLong();
}
bool operator >(const Time&t1, const Time&t2)
{
return t1.asLong() > t2.asLong();
}
bool operator ==(const Time &a, const Time &b)
{
return a.asLong() == b.asLong();
}
// Declare addition and Subtraction
Time operator +(const Time&) const
{
int a,b,c,d;
a = t1.hours+t2.hours;
b = t1.minutes+t2.minutes;
c = t1.seconds+t2.seconds;
d = t1.millis+t2.millis;
if (d > 999)
{
c = c+1;
d = d - 1000;
}
if (c > 59)
{
b = b + 1;
c = c - 60;
}
if (b > 59)
{
a = a+1;
b = b-60;
}
if (a > 24)
{
a = a - 24;
}
return Time(a,b,c,d);
}
Time operator -(const Time&) const
{
int a,b,c,d;
a = t1.hours-t2.hours;
b = t1.minutes-t2.minutes;
c = t1.seconds-t2.seconds;
d = t1.millis - t2.millis;
if (d < 0)
{
c = c -1;
d = d + 1000;
}
if (c < 0)
{
b = b - 1;
c = c + 60;
}
if (b < 0)
{
a = a + 1;
b = b - 60;
}
if (a < 24)
{
a = a + 24;
}
return Time(a,b,c,d);
}
我不能讓編輯.h文件。這是項目給我的。我可以對.h文件進行的唯一修改是添加私有成員函數。 –