没有匹配运算符<<在std :: cout [英] No match for operator << in std::cout

查看:326
本文介绍了没有匹配运算符<<在std :: cout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印一个向量内的每个元素,如下:

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> >]()' 

这是我错过的明显吗?

推荐答案

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

Have you defined a function with this signature?:

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

是用户的会员功能,可能不是朋友,取决于你。原型应该放在类用户的头文件中,正文应该放在源(.cpp)文件中。我不知道你的用户类是如何定义的,或者你想如何格式化输出,但函数定义应该看起来像这样:

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

这篇关于没有匹配运算符&lt;&lt;在std :: cout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆