2
std::ostream& operator<<(std::ostream&, const Course&);

void Course::display() {
    std::cout << std::left << courseCode_ << " | " << std::setw(20) << courseTitle_ << " | " << std::right
        << std::setw(6) << credits_ << " | " << std::setw(4) << studyLoad_ << " | ";
}

std::ostream& operator<<(std::ostream& os, const Course& a) {
    a.display();
    return os;
}

Problem occurs at the implementation of the ostream operator below a.display(). I don't see where the problem is, I have other codes that work with the same implementation.

error message:

The object has type qualifiers that are not compatible with the member function "sict::Course::display" object type is const sict::Course

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Andy
  • 127
  • 2
  • 9
  • possible duplicate of [link](http://stackoverflow.com/questions/24677032/object-has-type-qualifiers-that-are-not-compatible-with-the-member-function) – The Philomath Aug 05 '16 at 03:53
  • 2
    The `Course::display` function, why is it hardcoded to write to `std::cout`? What if you want to write to a file (which will be possible to do with the `operator<<` overload you have)? – Some programmer dude Aug 05 '16 at 03:56

1 Answers1

9

In operator<<(), a.display(); fails because a is declared as const. You can't call non-const member function on it.

Course::display() should be declared as const member function, it's supposed to not modify anything.

void Course::display() const {
//                     ^^^^^
    ...
}
songyuanyao
  • 169,198
  • 16
  • 310
  • 405