【发布时间】:2013-01-10 14:58:13
【问题描述】:
我正在尝试在模板链表中重载
简而言之,我有一个抽象类和两个继承他的派生类。 如果我使用 dynamic_cast,所有 3 个类都有运算符
但是当我尝试在模板链表中使用运算符
抽象类 - 播放器
#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
派生类之一
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);
基础和派生的两个
模板链表
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();
}
}
【问题讨论】:
-
我只是希望你的代码实际上没有像那样缩进。
-
这里不是我这样出来的
标签: c++ templates linked-list operator-overloading