No Operator

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.

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!

1

6 Answers

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

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

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

See the operator overloading bible here on StackOverflow for more information.

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

cout << c[i] << endl;

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

For example let assume that below is a definition of class 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;

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 Course is:

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

Your Course class needs to implement an operator:

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

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

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

Your problem is this particular part:

cout << c[i]

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

  1. implement the overloaded << operator for your object OR
  2. if your Course object is in someway a typedef to a primitive try explicitly casting it to a string type (or similar)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest