Trying to overload << for inner class

 ostream& elem::operator<<(ostream& cout_, const elem& ob) 

Returns an error:

:: must be class or namespace name

The class elem part of the set class.

in the description area

 friend ostream& operator << (ostream&, const elem&){}; 

All class code + function code (yes, I know there is a govnokod, I am correcting it now):

 class set{ public: set():head(NULL),tail(NULL),size(0){}; set(int sz); // конструктор ΠΏΠΎ-ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ set(const set &s); // ΠΊΠΎΠΏΠΈΡ€ΡƒΡŽΡ‰ΠΈΠΉ конструктор //ΠΎΡ‚Π½ΠΎΡˆΠ΅Π½ΠΈΡ set &operator =(const set &ob); //присваиваниС int operator <(set &ob); //подмноТСство int operator >(set &ob); //надмноТСство int operator <(int el); //ΠΏΡ€ΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ½ΠΎΡΡ‚ΡŒ элСмСнта int operator ==(set &ob); //ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½Ρ‚Π½ΠΎΡΡ‚ΡŒ int operator !=(set &ob) {return !(*this==ob);} //Π½Π΅ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½Ρ‚Π½ΠΎΡΡ‚ΡŒ //ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ Π½Π°Π΄ мноТСствами friend set operator +(int x, set &ob) {return ob+x;} set operator +(int el) {add(el); return *this;} //Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ элСмСнта set operator +(set &ob); //объСдинСниС мноТСств set operator &(set &ob); //пСрСсСчСниС мноТСств set operator -(set &ob); //Ρ€Π°Π·Π½ΠΎΡΡ‚ΡŒ set operator -(int x); //ΡƒΠ΄Π°Π»Π΅Π½ΠΈΠ΅ элСмСнта set operator ^(set &ob); //симмСтричСская Ρ€Π°Π·Π½ΠΎΡΡ‚ΡŒ set operator --(int x); //ΡƒΠ΄Π°Π»Π΅Π½ΠΈΠ΅ элСмСнта с ΠΊΠΎΠ½Ρ†Π° set operator --(); //ΡƒΠ΄Π°Π»Π΅Π½ΠΈΠ΅ элСмСнта ΠΈΠ· Π½Π°Ρ‡Π°Π»Π° int size() {return size;} //Ρ€Π°Π·ΠΌΠ΅Ρ€ мноТСства friend ostream& operator << (ostream&, const int&){}; private: int size; //Ρ€Π°Π·ΠΌΠ΅Ρ€ мноТСства class elem{ //связанный список для хранСния элСмСнтов мноТСства private: int data; elem* next; public: friend class set; elem():next(NULL){}; elem(int x):next(NULL),data(x){}; void add(int x); //Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ элСмСнта Π² список+ void del(int x); void clean(); elem* find(int x); friend ostream& operator << (ostream&, const elem&){}; }; elem *tail; //хвост списка elem *head; //Π³ΠΎΠ»ΠΎΠ²Π° списка }; 

function code

 ostream& set::elem::operator<<(ostream& cout_, const elem& ob) { cout_ << ob; return cout_; } 
  • Where is the definition, in the classroom or separately? - Nicolas Chabanovsky ♦
  • definition separately - Sergey
  • Give all the code. At the moment, there is an impression of incorrect work with the scope. - Nicolas Chabanovsky ♦
  • so this is a no brainer - Sergey

2 answers 2

Error here:

 friend ostream& operator << (ostream&, const elem&){}; ^^ ΠΏΠΎΡ‡Π΅ΠΌΡƒ пустоС Ρ‚Π΅Π»ΠΎ Ρ„-Ρ†ΠΈΠΈ? 
  • Yes, it's a joint, but it has nothing to do with a specific error, it has already been removed, everything is as it was. - Sergey
  • Then try changing <pre> ostream & set :: elem :: operator << (ostream & cout_, const elem & ob); </ pre> to <pre> ostream & set :: elem :: operator << (ostream & cout_, const set: : elem & ob); </ pre> - gecube
  • The problem seems to be in the parameters. removed all neymspeysy like normal, but Elem Inaccesable - Sergey
  • I was thinking here - and really, this is logical. This operator β€œis obtained is not a class of the class, but is defined outside, and then it is stated that he is a friend of our nested class so that it can change its private members. Those. <pre> ostream & set :: elem :: operator << (ostream & cout_, const elem & ob) ^^^^^^^^^^^^ superfluous </ pre> But in the second argument, you must specify the namespace, because . in the global namespace, there could also be a class elem. - gecube
  • So I added the namespace from the set there and it turned out that elem is inaccessible, it seems to me that I need to somehow stream the opportunity to look at this description, but I can’t figure out how. - Sergey

It seems to me that there is an error in scope. Try something like:

 ostream& set::elem::operator<<(ostream& cout_, const elem& ob); 
  • elem is inaccessible - Sergey