2017-04-24 112 views
0

所以我想建立一個類,可以保存有關原子元素的信息,然後對它們進行計算。我正在與我的重載*友元函數的錯誤,即變量atom_weight是函數的範圍內私有的,但它是一個朋友,所以它不應該是我的朋友功能沒有訪問私有變量

// The chemical class 
class Chemical{ 
     public: 
     Chemical(); 
     Chemical(string chemsym); 
     Chemical(string chemsym, int number, double weight); 
     void get_element(string chemsym, ifstream& fin); 
     void clear(); 
     friend Chemical operator +(Chemical& molecule, Chemical& element); 
     friend Chemical operator *(const Chemical element, int multiplier); 
     friend Chemical operator >>(istream& ins, Chemical element); 
     friend Chemical operator <<(ostream& outs, Chemical element); 
     string get_sym(); 
     int get_num(); 
     double get_weight(); 
     private: 
     string chemsym; 
     int atom_num; 
     double atom_weight; 
}; 

然後這裏是我的函數定義我的重載*操作符。

Chemical operator *(const Chemical& element, int multiplier){ 
     Chemical tempele; 
     string number; 
     tempele.atom_weight = element.atom_weight * multiplier; 
     number = itostr(mulitplier); 
     tempele.chemsym = element.chemsym + number; 
     return tempele; 
} 

我的大部分操作員都會得到類似的錯誤,但我的補充之一併不是我找不到任何區別。如果有人對如何解決這個問題有所瞭解。

+2

此功能與朋友聲明不符。檢查參數類型。 – user2357112

+0

你的「朋友」有點偏離... – InternetAussie

回答

0

函數聲明同時是函數定義不對應於類中的函數聲明。聲明功能如

friend Chemical operator *(const Chemical &element, int multiplier); 
              ^^