我正在尝试为嵌套类 ArticleIterator 重载 // ...class ArticleContainer { public: class ArticleIterator { // ... friend ostream& operator<<(ostream& out, const ArticleIterator& artit); }; // ...};如果我像往常一样定义 operatorfriend ostream& operator<<(ostream& out, const ArticleContainer::ArticleIterator& artit) {错误是 'friend' used outside of class 。我该如何解决? 最佳答案 定义函数时不要放置 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";}关于c++ - 运算符<< 用于嵌套类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4015401/ 10-15 00:29