No match for operator << in std::cout [duplicate]

淺唱寂寞╮ 提交于 2019-12-04 17:25:10

Have you defined a function with this signature?:

std::ostream & operator<<(std::ostream &, const users &);

It should not be a member function of users, although it may or may not be a friend, up to you. The prototype should go in the header file of class users, and the body should go in the source(.cpp) file. I have no idea how your users class is defined, or how you would want to format the output, but the function definition should look something like this:

std::ostream & operator<<(std::ostream & os, const users & U)
{
    os << U.some_data_members;
    os << U.and_or_some_member_functions();
    os << whatever;
    return os;
}

Once you've defined std::ostream &operator<<(std::ostream &, user&);, consider changing your code to use std::copy instead of a for loop:

// leaving off the `std::`, you're not using it for `cout`.
// 
copy(userlist.begin(), userlist.end(), ostream_iterator<user>(cout, "\n"));

Have you defined stream operator for the users class? If not, do so.

You need to write an overload of ostream::operator<<() that takes a an instance of users, or write some conversion operator that will provide an auto-conversion from user to some type that one of the operator<<() versions knows about.

You need to define your own public function operator<< taking parameters of an ostream and a users:

std::ostream& operator<<(std::ostream&, users&);

Sorry, is it users or user?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!