c++ - 如何修复 : binary = :no operator exists which takes a RHS of const & fullName

标签 c++ stl operator-keyword

我正在演示 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/

相关文章:

c++ - 运算符<<在C++中为类中的类重载

c++ - 如何从一组已定义的描述符中动态构建新的 protobuf?

c++ - 迭代器的默认值是多少?

Javascript ?运营商 : how to pass multiple parameters

C++ "group where"算法

c++ - 是否可以使用vector <T>作为值来定义unordered_map?

c++ - 一个结构中的多个点的运算符

c++是一种与空格无关的语言,规则的异常(exception)

c++ - 在编译时检测是否存在默认构造函数

使用自定义 "bind3rd"函数时,C++模板编译错误