【发布时间】:2013-08-18 20:52:46
【问题描述】:
我在链接类实现中重载了'对运算符的未定义引用
这是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;
并且错误出现在这一行的main函数中:
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
希望大家能帮帮我,非常感谢您的帮助
【问题讨论】:
-
你是在头文件中实现的吗?
-
比较你的声明和你的定义(第二个参数)。
-
为什么要将
current节点保留在实际的列表类中? -
另外,如果您询问有关编译器/链接器错误的问题,您也可以通过在问题中发布实际错误来提供帮助。请尽可能完整且未经编辑。
-
@Oleksiy 模板不是这样。
标签: c++ data-structures linked-list operator-overloading code-separation