I have a class CFoo with a private inner class CBar. I want to implement a stream ouput operator for CFoo, which in turn uses a stream output for CBar in it's implementation. I can get this working when CFoo is in the common namespace, but when i place it in a new namespace (namespace foobar), the operator can no longer access the private inner class. I suspect this has something to do with the full signature of the operator, but I can't figure out the correct way to specify the friend declaration and the actual operator declaration so the implementation compiles. Can anyone suggest what I might be missing? Note that it will compile if the stream implementation is done inline in the header, but I hate to expose implementation like this unnecessarily!
in foobar.h (just comment out the usefoobarnamespace to test the non-namespaced version):
#define usefoobarnamespace
#ifdef usefoobarnamespace
namespace foobar
{
#endif // usefoobarnamespace
class CFoo
{
public:
CFoo() {}
~CFoo();
void AddBar();
private:
class CBar
{
public:
CBar() {m_iVal = ++s_iVal;}
int m_iVal;
static int s_iVal;
};
std::vector<CBar*> m_aBars;
friend std::ostream& operator<<(std::ostream& rcStream, CFoo& rcFoo);
friend std::ostream& operator<<(std::ostream& rcStream, CFoo::CBar& rcBar);
};
std::ostream& operator<<(std::ostream& rcStream, CFoo& rcFoo);
std::ostream& operator<<(std::ostream& rcStream, CFoo::CBar& rcBar);
#ifdef usefoobarnamespace
}
#endif // usefoobarnamespace
and in foobar.cpp:
#ifdef usefoobarnamespace
using namespace foobar;
#endif // usefoobarnamespace
int CFoo::CBar::s_iVal = 0;
CFoo::~CFoo()
{
std::vector<CBar*>::iterator barIter;
for (barIter = m_aBars.begin(); barIter != m_aBars.end(); ++barIter)
{
delete (*barIter);
}
}
void CFoo::AddBar()
{
m_aBars.push_back(new CBar());
}
std::ostream& operator<<( std::ostream& rcStream, CFoo& rcFoo )
{
rcStream<<"CFoo(";
std::vector<CFoo::CBar*>::iterator barIter;
for (barIter = rcFoo.m_aBars.begin(); barIter != rcFoo.m_aBars.end(); ++barIter)
{
rcStream<<(*barIter);
}
return rcStream<<")";
}
std::ostream& operator<<( std::ostream& rcStream, CFoo::CBar& rcBar )
{
return rcStream<<"CBar("<<rcBar.m_iVal<<")";
}