Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对于',没有可行的过载=';当有接线员时_C++_Vector_Iterator_Operator Overloading - Fatal编程技术网

C++ 对于',没有可行的过载=';当有接线员时

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

myType对=有一个运算符重载,但在JSON类中调用它时,
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()