1

I'm trying to overload << operator within a template linked list.

in short I have one abstract class and 2 derived classes that inhered him. all 3 classes have operator << written & working well if I use dynamic_cast.

But when I try to use operator << within the template linked list I either get the address or a linker error for some reason.

Abstract class - Player

#ifndef PLAYER_H_
#define PLAYER_H_
#include <iostream>

class Player {
private:
  const char* m_name;
  int age;
public:
  static int numofPlayers;
  Player(const char*,int);
  virtual ~Player();
  friend std::ostream& operator<<(std::ostream& out, const Player &p);
};

std::ostream& operator<<(std::ostream& out, const Player &p);

#endif

One of the derived classes

class Goalkeeper:public Player {
private:
  int Saved_Goals;
  int Shirt_Number;
public:
  Goalkeeper(const char* name,int age, int number);
  ~Goalkeeper();

  friend std::ostream& operator<<(std::ostream& out, const Goalkeeper& s);
};

std::ostream& operator<<(std::ostream& out, const Goalkeeper& s);

Both of the << operators for base & derived work well! Only when I try to use them in the template linked list I get the address and not the data:

Template Linked List

template <typename T>
class Node {
  T* m_data;
  Node* next;
public:
  Node ();
  Node(T*, Node<T>*);
  ~Node();
  T* getData ()const {return this->m_data;};
  Node* getNext()const{return this->next;};
  template <typename T>
  friend std::ostream& operator<<(std::ostream &output, const Node<T>& Nd );
};

template <typename T>
std::ostream &operator << (std::ostream &output,const Node<T>& Nd ) {
  output<<Nd.m_data<<endl;
  return output;
}

template <typename T>
void List<T>::PrintMe() const {
  Node<T>* temp=this->head;
  while(temp) {
    cout<<*temp;
    temp=temp->getNext();
  }
}
2
  • I just hope your code is not actually indented like that. Commented Jan 26, 2013 at 23:07
  • it's not i came out like this in here Commented Jan 27, 2013 at 6:39

1 Answer 1

3

You are printing out the pointer. Use the dereference operator to print out the value.

output<<*(Nd.m_data)<<endl;

Also please indent your code before posting!

Question in comment:

It prints the data now but only for the base class and not the derived class for some reason

Because method overloading is based on the static type of an expression. Therefore with a List<Base>, the method called is operator<<(ostream&, const Base&). Try a List<Derived> and your code will work! You'll need a virtual function call that does the work for you in the operator<< of the base class to make a list containing both kind of elements work.

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

2 Comments

i'll pay more attention to the indentation next time ! It prints the data now but only for the base class and not the derived class for some reason i've edited the original question - added the output << operators for base & derived. Please have a look ...
Hey, still no go, any other ideas ?

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.