Working on a project I did not initiate, I want to add an <<
operator to a class. Problem: the class is a private inner class of an other class, the latter being in a namespace
.
And I cannot make it.
The problem can be simplified this way:
#include <iostream>
#include <map>
namespace A {
class B {
private:
typedef std::map<int, int> C;
C a;
friend std::ostream& operator<<(std::ostream& os, const C &c) {
for (C::const_iterator p = c.begin(); p != c.end(); ++p)
os << (p->first) << "->" << (p->second) << " ";
return os;
}
public:
B() {
a[13] = 10;
std::cout << a << std::endl;
}
};
}
int main() {
A::B c;
}
I try to compile it with g++ test.cpp
: error: no match for ‘operator<<’
. The compiler did not find my overloaded function. I thought it would have been simpler to define it in the header, with no luck. If you think it is more appropriate, I could also define the class in the CPP file, but I do not know how to do.
Last requirement, I cannot use C++11 (unfortunately).