Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operator<< for nested class

I'm trying to overload the << operator for the nested class ArticleIterator.

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

If I define operator<< like I usually do, I get a compiler error.

friend ostream& operator<<(ostream& out, const ArticleContainer::ArticleIterator& artit) {

The error is 'friend' used outside of class. How do I fix this?

like image 276
Pieter Avatar asked Dec 21 '22 22:12

Pieter


1 Answers

You don't put the friend keyword when defining the function, only when declaring it.

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";
}
like image 179
Peter Alexander Avatar answered Jan 02 '23 01:01

Peter Alexander