与运算符 << 不匹配在 std::cout 中

发布于 2024-11-02 09:51:50 字数 484 浏览 1 评论 0 原文

我试图像这样打印向量内的每个元素:

vector<users>::iterator i;

for(i = userlist.begin(); i<userlist.end(); i++)
{
        cout << *i << "\n";
}

然后我收到这样的错误:

no match for 'operator<<' in 'std::cout << (&i)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = users*, _Container = std::vector<users, std::allocator<users> >]()' 

我错过了什么明显的事情吗?

I'm trying to print every element inside a vector like this:

vector<users>::iterator i;

for(i = userlist.begin(); i<userlist.end(); i++)
{
        cout << *i << "\n";
}

Then I'm getting an error like this:

no match for 'operator<<' in 'std::cout << (&i)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = users*, _Container = std::vector<users, std::allocator<users> >]()' 

Is it anything obvious I've missed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心房敞 2024-11-09 09:51:50

您是否使用此签名定义了一个函数?:

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

它不应该是用户的成员函数,尽管它可能是也可能不是友元函数,具体取决于您。原型应该放在类用户的头文件中,主体应该放在源文件(.cpp)中。我不知道您的用户类是如何定义的,或者您希望如何格式化输出,但函数定义应该如下所示:

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

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;
}
鹤仙姿 2024-11-09 09:51:50

定义 std::ostream &operator<<(std::ostream &, user&); 后,请考虑更改代码以使用 std::copy 而不是 for 循环:

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

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"));
放赐 2024-11-09 09:51:50

您是否为用户类定义了流运算符?如果没有,就这样做。

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

故事和酒 2024-11-09 09:51:50

您需要编写一个带有 users 实例的 ostream::operator<<() 重载,或者编写一些提供自动转换的转换运算符从 useroperator<<() 版本之一知道的某种类型。

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.

你的心境我的脸 2024-11-09 09:51:50

您需要定义自己的公共函数运算符<<获取 ostream 和 users 的参数:

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

抱歉,是 users 还是 user

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文