2016-05-10 76 views
0

我正在嘗試編寫一個程序,在該程序中,我可以比較兩名員工總工資與重載大於運算符。爲了獲得總薪酬,我有一個會員職能來返回毛薪。出於某種原因,每當我去比較兩個總支付我​​得到:作爲非成員函數重載'>'

錯誤1錯誤C2662:「double employeeclass::Employee::grosspay(void)」:不能轉換「this」從「const employeeclass::Employee」指針「employeeclass::Employee &

2智能感知:所述對象具有鍵入不與成員函數的對象類型兼容限定符是:const employeeclass::Employee

我已經包含了給我的問題的代碼段。

在.cpp:

#include <fstream> 
#include <iomanip> 
#include <string> 

using namespace std; 
#include "Employee.h" 
namespace employeeclass { 
    bool operator> (const Employee &e1, const Employee &e2) { 
     if (e1.grosspay() > e2.grosspay()) 
      return true; 
     else 
      return false; 
    } 
} 

.h文件

namespace employeeclass { 
    class Employee { 
     friend bool operator> (const Employee &e1, const Employee &e2); 
    } 
} 
+5

聽起來像'grosspay()'函數沒有標記'const' –

+2

順便說一句if(X)return true;否則返回false;'可以更好地表示爲'return X;' –

+0

請編輯您的問題以包含[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve),最重要的是我們需要查看「Employee :: grosspay」的聲明。 –

回答

1

添加一個const,以頭和CPP文件grosspay()

double employeeclass::Employee::grosspay(void) const; 

限定符const確保實例在const聲明中使用該函數。基本上,const意味着這個函數不會進行更改並且是隻讀的。