我运行了一个程序,报出如下错:
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 &,int,int,int)" (??$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;
}
}
求大神解释一下,谢谢了