C++ 对于',没有可行的过载=';当有接线员时
myType对=有一个运算符重载,但在JSON类中调用它时,C++ 对于',没有可行的过载=';当有接线员时,c++,vector,iterator,operator-overloading,C++,Vector,Iterator,Operator Overloading,myType对=有一个运算符重载,但在JSON类中调用它时,it=js.allInfo.begin()编译器抛出:“对于“=”没有可行的重载” classjson{ 私人: 矢量allInfo; 公众: friend ostream&operator您正在尝试使用非常量迭代器迭代常量对象(const JSON&js) 使用常量迭代器: class JSON{ private: vector<myType> allInfo; public: friend ostream
it=js.allInfo.begin()代码>编译器抛出:“对于“=”没有可行的重载”
classjson{
私人:
矢量allInfo;
公众:
friend ostream&operator您正在尝试使用非常量迭代器迭代常量对象(const JSON&js)
使用常量迭代器:
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;
};
您正在尝试使用非常量迭代器迭代常量对象(constjson&js)
使用常量迭代器:
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){/*将数据从value复制到this*/;return*this;}
。注意返回类型和返回值是什么(不是原始的,而是*this
)js.allinfo.begin()
是vector的一个成员,与myType
无关。通常赋值运算符的返回类型是对该类型的引用,在这种情况下,它将是myType&
。您是从operator=
返回myType,而不是myType&
。不知道这是否对您有帮助……我已经这样做了const myType&value,它与const myType&value相同,如果您的意思是否定的,const
对于参数是正确的,但对于返回类型则不正确。myType&operator=(const myType&value){/*将数据从value复制到this*/;return*this;}
。注意返回类型和返回值是什么(不是原始版本,而是*此
)js.allinfo.begin()
是vector的一个成员,与myType
无关。更好的是,为循环使用范围。更好的是,为循环使用范围。
vector<myType>::const_iterator it;
auto it = js.allInfo.begin()