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;}
'operator <<'修改'cout << A;'中的'std :: cout'。這是[正確的簽名](http://stackoverflow.com/questions/4421706/operator-overloading/4421719#4421719)。 – chris