繁体   English   中英

在main中创建模板化类的实例时发生LNK2019错误

[英]LNK2019 error when creating an instance of templated class in main

我正在尝试为链接列表创建模板节点,但是在尝试构建解决方案时出现错误LNK2019:无法解析的外部符号。 仅当我尝试在Main中创建Node的实例时,才会发生这种情况。

Node.h:

#ifndef Node_A
#define Node_A

template <class T>
class Node
{
public:
 Node();
 ~Node();
 T getData();
 Node* getNext();
 void setData(T);
 void setNext(Node*);

private:
 Node *next;
 T data;
};
#endif

Node.cpp

#include "Node.h"

template <class T>
Node<T>::Node()
{
 next = NULL;
 return;
}

template <class T>
Node<T>::~Node()
{
 return;
}

...irrelevant

Main.cpp

#include <iostream>
#include "Node.h"

using namespace std;

int main()
{
 Node<int> a,b;
 //Node<int> *ptr;

 /*...*/

 return 0;
}

错误信息:

1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Node<int>::Node<int>(void)" (??0?$Node@H@@QAE@XZ) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Node<int>::~Node<int>(void)" (??1?$Node@H@@QAE@XZ) referenced in function _main

您的函数模板和模板类成员函数定义必须连同其声明一起放入头文件中。 除非您使用extern template ,否则在使用它的每个翻译单元中都必须定义函数模板或模板类成员函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM