我正在演示 C++ STL 和 ostream_iterators 与集合的用法。我有一个带有 setter/getter 和构造函数的简单类。每当我尝试使用 ostream_iterator 和复制函数时,我都会收到“二进制‘=’:未找到采用‘const fullName’类型的右手操作数的运算符(或者没有可接受的转换)”
我在 Visual Studio 2019 中编码
我已经尝试了几种运算符 = 的变体。我已经删除了 rhs 的 &,使函数无效,并编写了一个更复杂的版本来检查 lhs 和 rhs 是否相等。显然我用谷歌搜索了一下,然后点击了 MS 错误页面的链接。 我也知道我的集合已正确填充。我成功地迭代了集合。
这是我的类文件的一个片段。
class fullName {
protected:
string fname;
string lname;
public:
friend ostream& operator <<(ostream& out, const fullName& person) {
out << person.lname << ", " << person.fname;
return out;
}
fullName& operator = (const fullName& rhs) {
fname = rhs.fname;
lname = rhs.lname;
return *this;
}
friend bool operator <(fullName lhs, fullName rhs) {
return lhs.lname < rhs.lname; //simple cheesy sort by last name
}
friend bool operator >(fullName lhs, fullName rhs) {
return lhs.lname > rhs.lname; //simple cheesy sort by last name
}
};
复制行产生错误。
set <fullName, less <fullName>> people;
set <fullName>::iterator itr;
//populate from file
ostream_iterator<string> screen(cout, "\n"); //ostream object, delimiter
copy(people.begin(), people.end(), screen);
我希望我的输出被复制到屏幕上,就像我使用 int 或 string 类型集时所做的那样。
最佳答案
自 screen
类型为 ostream_iterator<string>
,您需要将 strings 分配给 screen
.由于没有来自 fullName
的转换至 string
,你会得到错误。
既然你在写 fullName
对象,您应该更改 screen
的声明到
ostream_iterator<fullName> screen(cout, "\n");
关于c++ - 如何修复 : binary = :no operator exists which takes a RHS of const & fullName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56452093/