的写法,在我按照自己理解写的时候出现了一些问题
template<typename DataType>
struct LinkNode{
DataType data;
LinkNode<DataType> *next;
explicit LinkNode(DataType value, ListNode<DataType> *ptr = nullptr){
// constructor function 构造函数
data = value;
next = ptr;
}
};
template<typename DataType>
class List {
protected:
LinkNode<DataType> *head;
public:
List();
void set_list(string &path);
};
template<typename DataType>
List<DataType>::List() {
// constructor function with no parameters
head = new LinkNode<DataType>;
head->data = 0;
}
template<typename DataType>
void List<DataType>::set_list(string &path) {
DataType number;
ifstream numberFile(path);
if (!numberFile){
cout << "Error! Load failed..." << endl;
exit(100);
}
while (numberFile >> number){
cout << number << ", ";
head = new LinkNode<DataType> (number, head);
}
}
LinkNode
结构体,用来构成主要的链表List
模板类,用来对链表进行相关操作List()
不带参数的构造函数set_list(&path)
对链表进行初始化,本来想写一个构造函数去直接对对象赋值,但是没实现 string path = "../Code/List.dat";
List<int> list(path);
但是这样的写法却是错误的,没办法像普通类一样实例化一个对象,请问如何按照头文件中的写法去实例化一个对象呢?
List<int> list(path);你没有这个构造函数怎么用这个?
你没有实现有参构造函数,只实现了一个set_list方法,需实现有参构造函数,或调用set_list方法初始化