C++ std::cout 中的“operator<<”不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15449508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:22:30  来源:igfitidea点击:

No match for 'operator<<' in std::cout

c++cout

提问by Kahn

I am developing gsoap web service where I am retrieving vectors of objects in return of a query. I have two ways to do it: first by simple loop and by iterator. None of them working.

我正在开发 gsoap Web 服务,我在其中检索对象的向量以返回查询。我有两种方法可以做到:首先通过简单循环和迭代器。他们都没有工作。

The error is:

错误是:

error: no match for 'operator<<'in 'std::cout mPer.MultiplePersons::info.std::vector<_Tp, _Alloc>::at<PersonInfo, std::allocator<PersonInfo> >(((std::vector<PersonInfo>::size_type)i))'

错误:与'operator<<'in不匹配'std::cout mPer.MultiplePersons::info.std::vector<_Tp, _Alloc>::at<PersonInfo, std::allocator<PersonInfo> >(((std::vector<PersonInfo>::size_type)i))'

MultiplePersons mPer; // Multiple Person is a class, containing vector<PersonInfo> info
std::vector<PersonInfo>info; // PersonInfo is class having attributes Name, sex, etc.
std::vector<PersonInfo>::iterator it;

cout << "First Name: \t";
cin >> firstname;
if (p.idenGetFirstName(firstname, &mPer) == SOAP_OK) {
    // for (int i = 0; i < mPer.info.size(); i++) {
    //    cout << mPer.info.at(i); //Error
    //}
    for (it = info.begin(); it != info.end(); ++it) {
        cout << *it; // Error
    }

} else p.soap_stream_fault(std::cerr);

}

It's obvious that operator overloading operator<<in coutis the problem. I have looked at several problems related to this, but no one helped me out. If someone can provide a concrete example on how to solve it, it would be very appreciated. (Please do not talk in general about it, I am new to C++ and I have spent three days on it searching for solution.)

很明显,操作符重载operator<<cout的问题。我已经查看了与此相关的几个问题,但没有人帮助我。如果有人可以提供有关如何解决它的具体示例,将不胜感激。(请不要笼统地谈论它,我是 C++ 的新手,我花了三天时间寻找解决方案。)

回答by juanchopanza

You need to provide an output stream operator for PersonInfo. Something like this:

您需要为 提供输出流运算符PersonInfo。像这样的东西:

struct PersonInfo
{
  int age;
  std::string name;
};

#include <iostream>
std::ostream& operator<<(std::ostream& o, const PersonInfo& p)
{
  return o << p.name << " " << p.age;
}

This operator allows expressions of the type A << B, where Ais an std::ostreaminstance (of which std::coutis one) and Bis a PersonInfoinstance.

此运算符允许类型为 的表达式A << B,其中A是一个std::ostream实例(其中std::cout是一个)并且B是一个PersonInfo实例。

This allows you do do something like this:

这允许你做这样的事情:

#include <iostream>
#include <fstream>
int main()
{
  PersonInfo p = ....;
  std::cout << p << std::endl; // prints name and age to stdout

  // std::ofstream is also an std::ostream, 
  // so we can write PersonInfos to a file
  std::ofstream person_file("persons.txt");
  person_file << p << std::endl;
}

which in turn allows you to print the de-referenced iterator.

这反过来又允许您打印取消引用的迭代器。

回答by Angew is no longer proud of SO

The result of *itis an L-value of type PersonInfo. The compiler is complaining that there is no operator<<which takes a right-hand side argument of type PersonInfo.

的结果*it是类型为 的 L 值PersonInfo。编译器抱怨没有operator<<which 接受类型为右侧的参数PersonInfo

For the code to work, you need to provide such an operator, for example like this:

要使代码正常工作,您需要提供这样的运算符,例如:

std::ostream& operator<< (std::ostream &str, const PersonInfo &p)
{
  str << "Name: " << p.name << "\nAge: " << p.age << '\n';
  return str;
}

The exact implementation of the operator depends on your needs for representing the class in output, of course.

当然,运算符的确切实现取决于您在输出中表示类的需要。

回答by Preet Sangha

What it's telling you is that there isn't a known wway to cout (console output) the contents of *it.

它告诉您的是,没有已知的方法来计算(控制台输出)*it 的内容。

itis an iterator - think of this like a pointer in a list

it是一个迭代器 - 把它想象成一个列表中的指针

the list is infoso *it is current item in the info, which is a list of PersonInfoitems.

列表是info如此 *它是 中的当前项目info,这是一个PersonInfo项目列表。

So cout << *it;says output to the console the PersonInfothat it is currently referencing.

所以cout << *it;说输出到控制台PersonInfo它当前正在引用。

But the error message is telling you that the compiler doens't know how PersonInfo should be rendered to the console.

但是错误消息告诉您编译器不知道应如何将 PersonInfo 呈现到控制台。

What you need to do is create an operator called <<that takes an object that coutis (ostream) and a PersonInfoobject and then writes the various bits of the PersonInfoto cout.

您需要做的是创建一个名为的运算符<<,它接受一个对象cout( ostream) 和一个PersonInfo对象,然后写入PersonInfoto的各个位cout