c++ - 范围界定错误并且不能有 cv 限定符?

标签 c++ class scope

我的问题与 Time 项目有关,我将在下面完整发布该项目以提供背景信息。我所做的大部分都是正确的,但是当涉及到加法和减法运算符定义时,我遇到了范围界定错误的问题。编译器找不到我的私有(private)成员。 (运算符用于如果用户在小时内输入 25,它会循环回到一。)整个项目正在创建已定义方法的实例。无论如何,第二个错误与运算符(operator)本身有关。我得到一个错误

 non-member function cannot have cv-qualifier

做研究告诉我这个错误是由于改变常量而发生的,在我的例子中是时间本身。我看不出我在哪里做的。我希望我很清楚,如果需要可以澄清。 .h 文件来自练习本身,除了添加私有(private)成员外,我无法更改它。

 #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);
   } 

最佳答案

以防万一,非成员函数是友元函数。在非成员函数中,没有this参数。他们不需要简历限定符。你为什么不改变:

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

关于c++ - 范围界定错误并且不能有 cv 限定符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46750860/

相关文章:

javascript - With block 中的变量声明

C++ 在 switch 语句中使用特定的类和枚举范围

c++ - 如果我想要最大速度,我应该只在 std::vector 上使用数组吗?

python - 使用类而不是列表 python

java - 创建一个类实例并使用局部变量命名它(android)

python - 类成员函数

java - ActionForm 内部的同步方法

c++ - 标准最新草案第 §8.5.3/5 段中的要点 (5.2.2.1) 中的 "user-defined conversions are not considered"是什么意思?

C++ Webkit GTK,如何禁用跨源策略?

c++ - gtkmm 管理/添加 vs 智能指针 :