2013-04-13 92 views
1

我试图重载< <运营商,但我发现这样的一些错误:在C++中重载<<运算符?

传递const std::ostream' as这个 '的std :: basic_ostream < _CharT,_Traits> &的std ::说法basic_ostream < _CharT, _Traits> ::运算< <(常量无效*)[与_CharT =炭,_Traits =标准:: char_traits]”丢弃限定符

这是我的代码:

#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;}  
+2

'operator <<'修改'cout << A;'中的'std :: cout'。这是[正确的签名](http://stackoverflow.com/questions/4421706/operator-overloading/4421719#4421719)。 – chris

回答

4

这 常量的ostream &操作< <(常量的ostream & O,LISTA & A)

应该是:

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

为实际流被修改,当你写吧。

+0

谢谢,现在工作! :) – user2277994