gpt4 book ai didi

c++ - 有运算符时 '=' 没有可行的重载

转载 作者:行者123 更新时间:2023-11-30 00:45:51 24 4
gpt4 key购买 nike

struct myType{
public:

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

};

myType 有一个运算符重载 = 但是当它在 JSON 类中调用时 it = js.allInfo.begin(); 编译器抛出:“'='没有可行的重载”

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

我应该在 overload= 中更改什么来解决这个问题

最佳答案

您正在尝试使用非 const 迭代器迭代 const 对象 (const JSON &js)。

使用常量迭代器:

vector<myType>::const_iterator it;

更好的是,使用关键字“auto”自动获取正确的类型:

auto it = js.allInfo.begin()

关于c++ - 有运算符时 '=' 没有可行的重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41213550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com