0

我有一个带有私有内部类 CBar 的 CFoo 类。我想为 CFoo 实现一个流输出运算符,它又在其实现中使用 CBar 的流输出。当 CFoo 在公共命名空间中时,我可以让它工作,但是当我将它放在新的命名空间(命名空间 foobar)中时,操作员不能再访问私有内部类。我怀疑这与运算符的完整签名有关,但我无法找出指定朋友声明和实际运算符声明的正确方法,以便编译实现。谁能建议我可能会错过什么?请注意,如果流实现在标头中内联完成,它将编译,但我讨厌不必要地公开这样的实现!

在 foobar.h 中(只需注释掉 usefoobarnamespace 以测试非命名空间版本):

#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

在 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<<")";
}
4

4 回答 4

2

只需将 .cpp 文件中的代码放入命名空间即可:

namespace foobar {

// your existing code

}
于 2009-08-21T09:29:57.413 回答
2

您需要将运算符定义明确地放在命名空间中。(或使用命名空间完全限定它们)。你这样做的方式是声明一些 << 运算符(位于命名空间 foobar 中),然后在全局命名空间中定义一些全新的 << 运算符。

namespace foobar
{
    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<<")";
    }
}
于 2009-08-21T09:30:24.447 回答
1

operator<< functions现在位于foobar命名空间中,因此您应该将它们定义为foobar::operator<<.

于 2009-08-21T09:29:32.360 回答
0

这个问题可以通过专门为命名空间重载流操作符来解决:

std::ostream& foobar::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& foobar::operator<<( std::ostream& rcStream, CFoo::CBar& rcBar )
{
    return rcStream<<"CBar("<<rcBar.m_iVal<<")";
} 

默认情况下,这些函数的全局定义会被重载。他们不是 CFoo 类的朋友,不能访问它的私有成员。

于 2009-08-21T09:41:51.803 回答