通用类c ++,定义了通用类

时间:2019-01-05 19:53:15

标签: c++ generic-list

因此,我试图理解c ++,到目前为止,这真是太难了!但仍然很有趣。我已经创建了一个班级学生,并且正在尝试创建一个通用链接列表班级。在应该对任何数据类型通用的节点类中,我将其定义为:

**header**
#include <iostream>
using namespace std;
template<class T>
class Node{
private: 
  T *data;
  Node *next,*prev;
public:
  Node(T*); 
};
**cpp**
#include "node.h"
template<class T>
Node<T>::Node(T *data){
 this->data = data;
 next = 0;
 prev = 0;
}
**main**
#include "student.h"
#include "node.h"
int main(){
 Student *s = new Student("student",123);
 Node<Student> *node = new Node<Student>(s);
}

但是随后我得到了我认为是链接器错误的信息,内容为:

"undefined reference to 'Node<Student>::Node(Student*)"

以下是我的Makefile:

output: node.o student.o main.o
        g++ node.o student.o main.o -o output
node.o: node.h node.cpp
        g++ -c node.cpp
student.o: student.h student.cpp
        g++ -c student.cpp
main.o: main.cpp
        g++ -c main.cpp
clean:
        rm *.o output

这应该很简单,但是我缺少一些概念。我有Java背景,有很多事情您都不会注意到,就像我在c ++中注意到的那样。预先感谢。

0 个答案:

没有答案