关于error LNK2019::Unresolved External Symbol * Referenced In Function..解决办法 2

前几天我转了一篇关于error LNK2019::Unresolved External Symbol * Referenced In Function..解决办法的博客,但我自己后来按照上面的办法并没有解决我遇到的问题,当然这里我不是在说原博主给出的解决方案不对,应该是我遇到的问题跟原博主提出的情况都不符合,所以才没解决!这里再次提一下我碰到的问题:
我是写了如下的代码:

 #ifndef CIRCLIST_H_
#define CIRCLIST_H_

template 
struct CircListNode{
    T data;
    CircListNode * link;
};

template 
class CircList{
private:
    CircListNode * first;
public:
    CircList(){}
    CircList(int i);
    ~CircList();
    CircListNode * Locate(int i);
};
#endif
 #include "stdafx.h"
#include "circList.h"
#include 

using namespace std;

template
CircList::CircList(int i)
{
    T value;
    first = new CircListNode < T > ;
    for (int j = 0; j < i; j++)
    {
        CircListNode * newNode = new CircListNode < T > ;
        cout << "请输入第" << j + 1 << "个节点的值:";
        getline(cin, value);
        newNode->data = valude;
        newNode->link = first->link;
        first->link = newNode;
    }
    cout << "一个包含" << i << "个节点的循环表建立成功!" << endl;
}

template
CircList::~CircList()
{
    CircListNode * q;
    while (first->link != NULL)
    {
        q = first->link;
        first->link = q->link;
        delete q;
    }
    delete first;
}

template
CircListNode * CircList::Locate(int i)
{
    CircListNode * 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;
    }
}
#include "stdafx.h"
#include 
#include "circList.h"

using namespace std;

template
void Josephus(CircList & js, int n, int s, int m);

int _tmain(int argc, _TCHAR* argv[])
{
    int n, s, m;
    cout << "请输入游戏人数、开始位置和报数间隔: ";
    cin >> n >> s >> m;
    CircList clist(n);
    Josephus(clist, n, s, m);
    return 0;
}

template
void Josephus(CircList & js, int n, int s, int m)
{
    CircListNode * 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;
    }
}

编译运行之后报出如下错误:

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

然后我去百度了很久,找到了挺多解决办法,但都没有解决,后来我又去看了《c++ primer plus》这本书,才知道自己其实犯了很致命又很简单的错误,就是我把模板类的声明和定义分别放在不同的文件中,这会导致问题,这是编译器所不允许的,所以我们只要我声明和定义写在一个.h文件里这问题就解决了!


希望这篇博客可以对大家有帮助!


你可能感兴趣的:(数据结构,c++学习记录)