【发布时间】:2014-05-27 09:34:16
【问题描述】:
伙计们,我正在尝试为我的类 SortedList 创建一个模板。我想重载
int main()
{
SortedList<int, int> lst, lst2;
int a = 2;
lst.addItem(2, 3);
cout << lst << endl;
return 0;
}
这是模板类的声明和定义
template <typename K, typename V>
struct Node
{
K key;
V value;
Node<K, V>* next;
};
template <typename K, typename V>
class SortedList
{
friend ostream& operator << <K, V>(ostream&, const SortedList&);
public:
SortedList();
SortedList(const SortedList&);
SortedList& operator = (const SortedList&);
~SortedList();
void addItem(const K&, const V&);
void removeElem(const K&);
void removeAt(int);
bool remove(const K&);
private:
Node<K, V>* start;
size_t n;
};
【问题讨论】:
-
friend ostream& operator << <K, V>(ostream&, const SortedList&);:如果你使用这样的语法,你应该在SortedList类模板定义之前和它的前向声明之后声明operator <<模板。
标签: c++ class templates operator-overloading friend-function