0

For class, I am trying to overload the << operator so I can print out an object I created. I declared and added to this

WORD you; //this is a linked list that contains 'y' 'o' 'u'

and I want to do this

cout << you; //error: no operator "<<" matches theses operands

I have to overload the insertion operator as a friend function with chaining to print a word.

I have declared and defined the overloaded function, but it still does not work. Here is the class declaration file, followed by the .cpp file with the function

#include <iostream>

using namespace std;
#pragma once

class alpha_numeric //node
{
public:
char symbol; //data in node
alpha_numeric *next;//points to next node
};

class WORD
{
public:
WORD(); //front of list initially set to 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 to the front node of a list
int length;

}; 

In the .cpp file, I put *front in the parameter because it said front was not defined when I tried to use it inside the function, even though I declared it in the class. I then tried this. I have no clue if it is correct.

ostream & operator<<(ostream & out, alpha_numeric *front)
{
alpha_numeric *p;
for(p = front; p != 0; p = p -> next)
{
    out << p -> symbol << endl;
}
}
2
  • 1
    Why don't you create ostream& operator<<(ostream &out, WORD &word) ? Commented Jun 5, 2012 at 2:15
  • @SevaTitov It has to be implemented as a friend function, its in the homework directions Commented Jun 5, 2012 at 2:18

1 Answer 1

1

If you want to overload << for class WORD, the parameter must be 'WORD' type. I think you must search overload for << 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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

can you explain why it has to be const?
@MikeGordon you do not want to make any change to WORD in operator<<, right? that's the reason.
Thanks, @JsDoITao for your solution, I have other questions about this content. If the members such as symbol and next in-class alpha_numeric were private instead of public how we can handle them? I am new in C++ as well, and I have this issue but in class alpha_numeric symbol and next are private members and I gave the error of symbol and next are private in this content.

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.