2017-04-24 70 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); 
              ^^