2011-04-17 156 views
1

我在使用重載的運算符時,在已經重載的運算符中遇到了一些問題。在我的下面的代碼中,我已經超載了運算符來比較兩個課程對象。反過來,運營商進入它調用其他重載運營商私有對象變量比較該對象的給他們比較主要的功能:C++運算符在已重載的運算符中重載

代碼:現在

bool operator&&(const DaysOfWeek& a, const DaysOfWeek& b); 
bool operator&&(const TimeInterval& a, const TimeInterval& b); 

,我的問題。我在這個項目中一直使用許多重載操作符,但這是我第一次不得不在其他重載操作符內調用重載操作符。不幸的是,我的重載操作符在上面的代碼中沒有被我的isOverlap函數調用。所以我的問題是:這是爲什麼,我該如何糾正它?

任何幫助將非常感謝,因爲我正在撞牆試圖讓這個工作。我已經包含Course.h中的相關代碼以及Course.cpp中的函數和重載運算符。我有粗體的代碼行,我有不規則的輸出(不使用我的重載操作符)。

代碼:

bool Course::isOverlap(const Course& b) const 
{ 
    DaysOfWeek tempDays = b.getDays(); 
    TimeInterval tempTime = b.getTime(); 
    if(this->instructor==b.getInstructor() && 
     &this->days&&(&tempDays) && 
     &this->time&&(&tempTime)) 
    { 
     return true; 
    } 
    else 
     return false; 
} 

bool operator&&(const Course& a, const Course& b) 
{ 
    if (a.isOverlap(b)) 
     return true; 
    else 
     return false; 
} 

代碼:

#ifndef COURSE_H 
#define COURSE_H 

#include <string> 
#include "TimeInterval.h" 
#include "DaysOfWeek.h" 

using namespace std; 

class Course 
{ 
    public: 
     Course(); 
     Course(const string courseCode, const string section, 
      const DaysOfWeek& days, const TimeInterval& time, 
      const string instructor); 
     void setCourse(string courseCode, string section, 
      DaysOfWeek& days, TimeInterval& time, string instructor); 
     string getCourse() const; 
     string getSection() const; 
     DaysOfWeek getDays() const; 
     TimeInterval getTime() const; 
     string getInstructor() const; 
     bool isOverlap(const Course& b) const; 
     bool isMatch(const Course& b) const; 

    private: 
     string courseCode; 
     string section; 
     DaysOfWeek days; 
     TimeInterval time; 
     string instructor; 
}; 

bool operator&&(const Course& a, const Course& b); 
bool operator==(const Course& a, const Course& b); 

#endif //COURSE_H 

我也試圖取代我的代碼:

bool Course::isOverlap(const Course& b) const 
{ 
    DaysOfWeek tempDays = b.getDays(); 
    TimeInterval tempTime = b.getTime(); 
    if(instructor==b.getInstructor() && 
     days && tempDays && 
     time && tempTime) 
    { 
     return true; 
    } 
    else 
     return false; 
} 

作爲一個朋友曾建議,但是這並未」 t甚至編譯(與超載的參數不匹配& &運營商)。

+0

你真的不想重載'&&'操作符。你的意思是重載'==',也許? – 2011-04-17 03:26:35

+0

不,我的意思是&&。對於作業,我必須使用&&來測試重疊。 ==完全匹配。 – Aurum 2011-04-17 03:33:05

回答

3

此:

instructor==b.getInstructor() && days && tempDays && time && tempTime 

等效於此:

(((((instructor==b.getInstructor()) && days) && tempDays) && time) && tempTime) 

首先instructor==b.getInstructor()進行評價,得到bool。然後編譯器看到&& days並嘗試找到&&的過載,其中需要boolDaysOfWeek。沒有一個,所以它會產生一個錯誤。

要使用重載&&旁邊的內置&&,你需要一些小括號強制子表達式分組:

instructor==b.getInstructor() && (days && tempDays) && (time && tempTime) 
           ^   ^^   ^

這就是說,我會強烈建議回去給你的老師,並告訴他,他瘋了。過載&&運營商(或||運營商)幾乎總是錯誤的,因爲它打破了運營商的正常語義(當超載時,這兩個運營商停止短路)。

+0

非常好!非常感謝您的回答。現在完美的工作!我無法相信這是一些括號問題,但是我再也沒有想過要像你說的那樣重載&&操作符。 ;) 再次,非常感謝! – Aurum 2011-04-17 03:48:00

1
DaysOfWeek tempDays = b.getDays(); 
    TimeInterval tempTime = b.getTime(); 
    if(this->instructor==b.getInstructor() && 
     &this->days&&(&tempDays) && 
     &this->time&&(&tempTime)) 

在上面使用的是邏輯和的days地址相比,tempDays地址,你應該比較對象,而不是地址。與timetempTime一樣。