C++ 操作员<&书信电报;对于嵌套类

C++ 操作员<&书信电报;对于嵌套类,c++,operator-overloading,friend,C++,Operator Overloading,Friend,我试图重载在定义函数时,您不需要放置friend关键字,只需要在声明函数时 struct A { struct B { friend std::ostream& operator<<(std::ostream& os, const B& b); }; }; std::ostream& operator<<(std::ostream& os, const A::B& b) { return os <<

我试图重载在定义函数时,您不需要放置
friend
关键字,只需要在声明函数时

struct A
{
 struct B
 {
  friend std::ostream& operator<<(std::ostream& os, const B& b);
 };
};

std::ostream& operator<<(std::ostream& os, const A::B& b)
{
 return os << "b";
}
结构A { 结构B {
friend std::ostream&operator在声明中使用friend关键字指定此函数/类为friend。在类外的定义中,您不能使用该关键字。只需将其删除即可

您必须在类内将其声明为friend,然后在类外不使用friend关键字进行定义

class ArticleContainer {
public:
    class ArticleIterator {
                    // ...
            friend ostream& operator<<(ostream& out, const ArticleIterator& artit);
    };
};

// No 'friend' keyword
ostream& operator<<(ostream& out, const ArticleIterator& artit);
类容器{
公众:
类迭代器{
// ...

friend ostream&Operator仔细想想,这与声明和定义无关。您可以在类内按词汇定义friend,也可以在类外重新声明函数,而不使用friend关键字。我认为这与特定声明的出现位置有关-friend说明符只能应用于函数decl类定义中的语法
struct A
{
 struct B
 {
  friend std::ostream& operator<<(std::ostream& os, const B& b);
 };
};

std::ostream& operator<<(std::ostream& os, const A::B& b)
{
 return os << "b";
}
class ArticleContainer {
public:
    class ArticleIterator {
                    // ...
            friend ostream& operator<<(ostream& out, const ArticleIterator& artit);
    };
};

// No 'friend' keyword
ostream& operator<<(ostream& out, const ArticleIterator& artit);