c++ - How to overload operator << with linked list? -
for class, trying overload << operator can print out object created. declared , added this
word you; //this linked list contains 'y' 'o' 'u' and want this
cout << you; //error: no operator "<<" matches theses operands i have overload insertion operator friend function chaining print word.
i have declared , defined overloaded function, still not work. here class declaration file, followed .cpp file function
#include <iostream> using namespace std; #pragma once class alpha_numeric //node { public: char symbol; //data in node alpha_numeric *next;//points next node }; class word { public: word(); //front of list set null //word(const word& other); bool isempty(); //done int length(); void add(char); //done void print(); //dont //void insert(word bword, int position); //word operator=(const string& other); friend ostream & operator<<(ostream & out, alpha_numeric *front);//******************<----------------- private: alpha_numeric *front; //points front node of list int length; }; in .cpp file, put *front in parameter because said front not defined when tried use inside function, though declared in class. tried this. have no clue if correct.
ostream & operator<<(ostream & out, alpha_numeric *front) { alpha_numeric *p; for(p = front; p != 0; p = p -> next) { out << p -> symbol << endl; } }
if want overload << class word, parameter must 'word' type. think must search overload << 1stly before asking such question. :-)
class word { friend ostream & operator<<(ostream & out, const word& w); } ostream & operator<<(ostream & out, const word& w) { alpha_numeric *p; for(p = w.front; p != 0; p = p -> next) out << p -> symbol; out<<endl; return out; }
Comments
Post a Comment