C++ 没有运算符 << 匹配这些操作数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21996995/
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 23:53:34  来源:igfitidea点击:

No operator << matches these operands

c++vector

提问by jlee

I've been reading questions here for an hour or two regarding this error I'm getting and most of them forgot to #include string (which I had already done), or to overload the << operator.

我已经在这里阅读了关于我遇到的这个错误的问题一两个小时,他们中的大多数人忘记了 #include 字符串(我已经这样做了),或者重载了 << 运算符。

Here's the code in question:

这是有问题的代码:

void Student::getCoursesEnrolled(const vector<Course>& c)
{
    for (int i = 0; i < c.size(); i++)
    {
        cout << c[i] << endl;
    }

}

And the error I'm getting:

我得到的错误是:

Error: No operator matches these operands
    operand types are: std::ostream << const Course

All I'm trying to do is return the vector. I read about overloading the << operator but we haven't learned any of that in class so I'm assuming there is another way of doing it?

我要做的就是返回向量。我读过重载 << 运算符,但我们在课堂上没有学到任何内容,所以我假设还有另一种方法可以做到这一点?

I appreciate your time!

我很感激你的时间!

回答by TypeIA

All I'm trying to do is return the vector.

我要做的就是返回向量。

Not quite; you're trying to printit using cout. And couthas no idea how to print a Courseobject, unless you provide an overloaded operator<<to tell it how to do so:

不完全的; 你想打印使用它cout。并且cout不知道如何打印Course对象,除非您提供一个重载operator<<来告诉它如何打印:

std::ostream& operator<<(std::ostream& out, const Course& course)
{
    out << course.getName(); // for example
    return out;
}

See the operator overloading biblehere on StackOverflow for more information.

有关更多信息,请参阅StackOverflow 上的运算符重载圣经

回答by D Stanley

The stream operator <<is used to "output" some representation of that object. If you don't want to overload the operator yet just pick some property to output instead:

流运算符<<用于“输出”该对象的某些表示。如果您不想重载运算符,只需选择一些属性来输出:

for (int i = 0; i < c.size(); i++)
{
    cout << c[i].Name << endl;  // assuming Name is a property of Course
}

When you DO overload the operator you just decide then what the proper representation of a Courseis:

当您重载运算符时,您只需决定 a 的正确表示Course是什么:

ostream& operator<< (ostream &out, Course &c)
{
    out << c.Name "(" << c.Description << ")";
    return out;
}

回答by Moo-Juice

Your Courseclass needs to implement an operator:

你的Course类需要实现一个运算符:

class Course
{
public:
    /* 
     * Your code here
     */

   // Probably missing this:
   friend std::ostream& operator << (std::ostream& os, const Course& course)
   {
      os << course.name(); // etc..
      return os;
   };
}; // eo class Course

回答by Tristan Brindle

Since you haven't yet learned to overload operator<<, what you can do instead is to print each member of your Courseclass. You haven't posted the definition of Course, but perhaps it's something like this:

由于您还没有学会重载operator<<,您可以做的是打印Course类的每个成员。您还没有发布 的​​定义Course,但也许是这样的:

class Course
{
public:
    int get_number() { return _number; }
    const std::string& get_name() { return _name; }

private:
    int _number;
    std::string _name;
};

then you can say:

那么你可以说:

void Student::getCoursesEnrolled(const vector<Course>& c)
{
    for (int i = 0; i < c.size(); i++)
    {
        cout << c[i].get_number() << " "
             << c[i].get_name() << std::endl;
    }
}

回答by Vlad from Moscow

The problem is that operator << is not overload for type Courseobjects of which you are trying to output in statement

问题是对于Course您尝试在语句中输出的类型对象,运算符 << 不会重载

cout << c[i] << endl;

You need to overload this operator or write your own function that will output an object of type Coursein std::ostream

您需要重载此运算符或编写自己的函数来输出类型为Coursein的对象std::ostream

For example let assume that below is a definition of class Course

例如假设下面是类的定义 Course

class Course
{
private:
   std::string name;
   unsigned int duration;
public:
   Course() : duration( 0 ) {}
   Course( const std::string &s, unsigned int n ) : name( s ), duration( n ) {}
   std::ostream & out( std::ostream &os ) const
   {
      return ( os << "Course name = " << name << ", course duration = " << duration );
   }
};

When you can write

什么时候可以写

std::vector<Course> v = { { "A", 1 }, { "B", 2 }, { "C", 3 } };

for ( const Course &c : v ) c.out( std::cout ) << std::endl;

Instead member function out you can overload operator <<. For example

取而代之的是成员函数,您可以重载运算符 <<。例如

class Course
{
private:
   std::string name;
   unsigned int duration;
public:
   Course() : duration( 0 ) {}
   Course( const std::string &s, unsigned int n ) : name( s ), duration( n ) {}
   friend std::ostream & operator <<( std::ostream &os, const Course & c )
   {
      return ( os << "Course name = " << c.name << ", course duration = " << c.duration );
   }
};

and use it as

并将其用作

std::vector<Course> v = { { "A", 1 }, { "B", 2 }, { "C", 3 } };

for ( const Course &c : v ) std::cout << c << std::endl;

回答by Taner Selim

Your problem is this particular part:

你的问题是这个特殊的部分:

cout << c[i]

cout << c[i]

In your case c[i]is an object of type Courseas dvnrrs correctly pointed out. So either:

在您的情况下,c[i]Coursedvnrrs 正确指出的类型的对象。所以要么:

  1. implement the overloaded << operator for your object OR
  2. if your Courseobject is in someway a typedefto a primitive try explicitly casting it to a string type (or similar)
  1. 为您的对象实现重载的 << 运算符 OR
  2. 如果您的Course对象在某种程度上是typedef原始类型,请尝试将其显式转换为字符串类型(或类似类型)