简体   繁体   English

有操作员时,'='没有可行的重载

[英]No viable overload for '=' when there is an operator

struct myType{
public:    

    myType operator=(const myType &value){
        return value;
    };

};

myType has an operator overload for = but when it is called in the JSON class at it = js.allInfo.begin(); myType对于=有一个运算符重载,但在JSON类中以it = js.allInfo.begin();调用时,会重载it = js.allInfo.begin(); compiler throws: "No viable overload for '='" 编译器抛出:“'='没有可行的重载”

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;
};

What should I change in the overload= to fix this problem 我应该在重载=中更改什么以解决此问题

You're trying to iterate a const object (const JSON &js) with a non-const iterator. 您正在尝试使用非常量迭代器迭代const对象(const JSON&js)。

Use a const iterator: 使用const迭代器:

vector<myType>::const_iterator it;

Better yet, use the keyword "auto" to automatically get the proper type: 更好的是,使用关键字“ auto”自动获取正确的类型:

auto it = js.allInfo.begin()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM