我的问题与时间项目有关,我将在下面全文发布以提供上下文。我所做的大部分内容都是正确的,但是当涉及到加法和减法运算符定义时,我遇到了范围错误的问题。编译器找不到我的私有成员。 (操作符适用于如果用户在小时内输入25,它会循环回到一个。)整个项目正在创建已定义方法的实例。无论如何,第二个错误,与运营商本身有关。我收到错误
non-member function cannot have cv-qualifier
做研究告诉我,这个错误是由改变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);
}
答案 0 :(得分:0)
如果非成员函数是friend
函数。在非成员函数中,没有this
参数。他们不需要cv-qualifier。你为什么不改变:
friend bool operator <(const Time&, const Time&);
friend bool operator >(const Time&, const Time&);
friend bool operator ==(const Time &a, const Time &b);
到
bool operator <(const Time &a);
bool operator >(const Time &a);
bool operator ==(const Time &a);