与运算符 << 不匹配在 std::cout 中
我试图像这样打印向量内的每个元素:
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> >]()'
我错过了什么明显的事情吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否使用此签名定义了一个函数?:
它不应该是用户的成员函数,尽管它可能是也可能不是友元函数,具体取决于您。原型应该放在类用户的头文件中,主体应该放在源文件(.cpp)中。我不知道您的用户类是如何定义的,或者您希望如何格式化输出,但函数定义应该如下所示:
Have you defined a function with this signature?:
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 &, user&);
后,请考虑更改代码以使用std::copy 而不是
for
循环:Once you've defined
std::ostream &operator<<(std::ostream &, user&);
, consider changing your code to usestd::copy
instead of afor
loop:您是否为用户类定义了流运算符?如果没有,就这样做。
Have you defined stream operator for the users class? If not, do so.
您需要编写一个带有
users
实例的ostream::operator<<()
重载,或者编写一些提供自动转换的转换运算符从user
到operator<<()
版本之一知道的某种类型。You need to write an overload of
ostream::operator<<()
that takes a an instance ofusers
, or write some conversion operator that will provide an auto-conversion fromuser
to some type that one of theoperator<<()
versions knows about.您需要定义自己的公共函数运算符<<获取 ostream 和 users 的参数:
抱歉,是
users
还是user
?You need to define your own public function operator<< taking parameters of an ostream and a users:
Sorry, is it
users
oruser
?