我在链接类实现中重载了'<<'运算符,我在主函数中声明了.cpp文件,但它不起作用,并且发生此错误: 未定义对运算符<&列表的引用)
这是linkedlist.h文件中ostream函数的声明:
friend std::ostream& operator<< (std::ostream& os, LinkedList<T>& list);
这是 ostream 函数的实现:
template <typename T>
std::ostream& operator<< (std::ostream& os, LinkedList<T> list)
{
list.current = list.start;
while(list.current != NULL)
{
os<< list.current->info<<" -> ";
list.current = list.current->next;
}
os<<"NULL"<<endl;
return os;
}
在主函数中,我创建了一个包含来自 SavingAccount 类的对象的列表
LinkedList <SavingAccount> list;
并且错误发生在这一行的主函数中:
cout << list <<endl;
好吧..这是 LinkedList 类的实现:
#include "LinkedList.h"
#include "SavingAccount.h"
#include <cstddef>
using namespace std;
template <typename T>
LinkedList<T>::LinkedList()
{
start = NULL;
current = NULL;
}
template <typename T>
LinkedList<T>::~LinkedList()
{
// Add code.
}
template <typename T>
std::ostream& operator<< (std::ostream& os,const LinkedList<T>& list) {
list.current = list.start;
while(list.current != NULL)
{
os<< list.current->info<<" -> ";
list.current = list.current->next;
}
os<<"NULL"<<endl;
return os;
}
这是 LinkedLins 类的头文件:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include<iostream>
using namespace std;
template <typename T>
struct Node{
T info;
Node<T> *next;
};
template <typename T>
class LinkedList
{
Node<T> *start;
Node<T> *current;
public:
LinkedList();
~LinkedList();
friend std::ostream& operator<< (std::ostream& os, const LinkedList<T>& list);
};
#endif // LINKEDLIST_H
我希望你们能帮助我,非常感谢你们的帮助