1

I'm trying to overload the << operator but I'm getting some errors of this kind:

passing const std::ostream' asthis' argument of `std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits]' discards qualifiers

Here is my code:

#include<iostream>
using namespace std;

class nod{
    protected: 
    int info;
    nod *next;
    friend class lista;
    friend const ostream &operator<<(const ostream &,lista&);

};

class lista
{nod *first, *last;
 public:
 lista()
 {first=new nod;
  last=new nod;
  first=last=NULL;}   
 void insert(int);
 // void remove();
  void afisare();
 nod *get_first(){ return first;};
};

void lista::insert(int x)
{    nod *nou=new nod;    
     nou->info=x;    
     if(!first)
                     first=last=nou;
     else         
                     nou->next=first;  
     first=nou;
     last->next=first;}


const ostream &operator<<(const ostream &o,lista &A)
{nod *curent=new nod;
o<<"Afisare: ";
curent=A.get_first();
if(curent)
          o<<curent->info<<" ";
curent=curent->next;
while(curent!=A.get_first())
          {o<<curent->info<<" ";
          curent=curent->next;}
return o;
}



int main()
{lista A;
A.insert(2);
A.insert(6);
A.insert(8);
A.insert(3);
A.insert(5);
cout<<A;
system("pause");
return 0;}    
1
  • 2
    operator<< modifies std::cout in cout << A;. Here is the proper signature. Commented Apr 13, 2013 at 17:51

1 Answer 1

4

This const ostream &operator<<(const ostream &o,lista &A)

should be:

ostream &operator<<(ostream &o,lista &A)

as the actual stream is modified when you write to it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.