伙计们,我正在尝试为我的类 SortedList 创建一个模板。我想重载 << 运算符,所以我在类中声明了一个友元函数,但每次我尝试编译 main.cpp 时都会出现以下错误:template-id 'operator<< ...' does not match any模板声明。这是来自 main.cpp 的代码
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;
};