0

I have two (let's say Class A and Class B) classes and one namespace (let's say project1). Class A is using std::vector to store many of Class B.

I have an operator overload to print Class B (essentially one item of Class A). However, my overload is somehow not being found. The compiler thinks I am trying to print a char (it checks every type besides my user-defined Class).

The error I get:

out << item << ", ";
error: invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>') and 'project1::B')

a.h

namespace project1 {
    class A {
        public:
            ...
            std::ostream& operator <<(std::ostream& out) const; 
        private:
            ...
    };
}

A.cpp

namespace project1 {
    ...
    std::ostream& A::operator <<(std::ostream& out) const {
        out << "[";
        // list is a private member of Class A
        for (int i = 0; i < list.size() - 1; i++) {
            project1::B item = list[i];
            out << item << ", ";
        }
        project1::B item = list[list.size() - 1];
        out << item << "]";
        return out;
    }
}
Josh Goldberg
  • 189
  • 1
  • 11

0 Answers0