【发布时间】:2017-05-03 23:18:33
【问题描述】:
struct myType{
public:
myType operator=(const myType &value){
return value;
};
};
myType 对 = 有一个运算符重载,但是当它在 it = js.allInfo.begin(); 的 JSON 类中调用时,编译器会抛出:“'=' 没有可行的重载”
class JSON{
private:
vector<myType> allInfo;
public:
friend ostream &operator<<(ostream &os,const JSON &js)
{
vector<myType>::iterator it;
for(it = js.allInfo.begin(); it != js.allInfo.end();it++){
cout << "this is the info "<<(it->getNAME()) << endl;
}
return os;
};
我应该在重载=中更改什么来解决这个问题
【问题讨论】:
-
通常赋值运算符的返回类型是对该类型的引用,在本例中为
myType&。您从operator=返回myType而不是myType&。不知道对你有没有帮助... -
我已经做了 const myType &value,和 const myType& value 一样,如果是你的意思
-
不,
const对于参数是正确的,但对于返回类型不正确。 -
myType& operator=(const myType &value) { /*copy the data from value into this */; return *this; }。注意返回类型和返回值是什么(不是原来的,而是*this) -
js.allinfo.begin()是vector的成员,与myType无关
标签: c++ vector iterator operator-overloading