Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No operator << matches these operands

Tags:

c++

vector

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!

like image 611
jlee Avatar asked Feb 24 '14 19:02

jlee


1 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.

like image 92
TypeIA Avatar answered Oct 17 '22 02:10

TypeIA