问题描述
- 关于c++ error LNK2019: unresolved external symbol 问题
- 我运行了一个程序,报出如下错:
Error 1 error LNK2019: unresolved external symbol ""public: __thiscall CircList::CircList(int)"" (??0?$CircList@N@@QAE@H@Z) referenced in function _wmain
Error 2 error LNK2019: unresolved external symbol ""public: __thiscall CircList::~CircList(void)"" (??1?$CircList@N@@QAE@XZ) referenced in function _wmain
Error 3 error LNK2019: unresolved external symbol ""public: struct CircListNode * __thiscall CircList::Locate(int)"" (?Locate@?$CircList@N@@QAEPAU?$CircListNode@N@@H@Z) referenced in function ""void __cdecl Josephus(class CircList &intintint)"" (??$Josephus@N@@YAXAAV?$CircList@N@@HHH@Z)Error 4 error LNK1120: 3 unresolved externals
程序代码如下:#ifndef CIRCLIST_H_#define CIRCLIST_H_template <class T>struct CircListNode{ T data; CircListNode<T> * link;};template <class T>class CircList{private: CircListNode<T> * first;public: CircList(){} CircList(int i); ~CircList(); CircListNode<T> * Locate(int i);};#endif
#include ""stdafx.h""#include ""circList.h""#include <iostream>using namespace std;template<class T>CircList<T>::CircList(int i){ T value; first = new CircListNode < T > ; for (int j = 0; j < i; j++) { CircListNode<T> * newNode = new CircListNode < T > ; cout << ""请输入第"" << j + 1 << ""个节点的值:""; getline(cin value); newNode->data = valude; newNode->link = first->link; first->link = newNode; } cout << ""一个包含"" << i << ""个节点的循环表建立成功!"" << endl;}template<class T>CircList<T>::~CircList(){ CircListNode<T> * q; while (first->link != NULL) { q = first->link; first->link = q->link; delete q; } delete first;}template<class T>CircListNode<T> * CircList<T>::Locate(int i){ CircListNode<T> * current = new CircListNode < T > ; if (current == NULL) { cout << ""内存分配错误!"" << endl; return NULL; } else { current = first->link; for (int j = 1; j < i - 1; j++) current = current->link; return current; }}
// ex2-5.cpp : Defines the entry point for the console application.//#include ""stdafx.h""#include <iostream>#include ""circList.h""using namespace std;template<class T>void Josephus(CircList<T> & js int n int s int m);int _tmain(int argc _TCHAR* argv[]){ int n s m; cout << ""请输入游戏人数、开始位置和报数间隔: ""; cin >> n >> s >> m; CircList<double> clist(n); Josephus(clist n s m); return 0;}template<class T>void Josephus(CircList<T> & js int n int s int m){ CircListNode<T> * p = js.Locate(s) *pre = NULL; for (int i = 0; i < n - 1; i++) { for (int j = 1; j < m; j++) { pre = p; p = p->link; } cout << ""出列的人是"" << p->data << endl; pre->link = p->link; p = pre->link; }}
求大神解释一下,谢谢了
解决方案
模板函数的定义原则上应该和声明放在同一个文件里。你的这里,虽然代码里有
#include ""circList.h""
但是编译器将模板实例化的时候找不到构造和析构函数的定义,因此才出现unresolved external symbol的问题。
你可以把你的函数定义加在头文件里,你也可以上网搜一下其他的模板函数的写法,比如我一直认为很别扭的#include ""xxx.cpp"" ...
解决方案二:
main.obj : error LNK2019: unresolved external symbol ""public: virtual __thiscall CvSVM::~CvSVM(void)"" (??1CvSVM@@UAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresol......
答案就在这里:error LNK2019: unresolved external symbol 问题
解决方案三:
CircList定义有问题
解决方案四:
模板函数声明在circList.h,而实现在circList.cpp,编译就会报错,原因为:模板函数在编译中就会实例化,把声明和定义分开,在一个编译单元内,及头文件展开后。在编译的时候虽然能找到函数声明,但是没有定义。
参考:http://blog.sina.com.cn/s/blog_5c8b05eb0100xzjg.html
时间: 2024-10-28 13:41:52