baidu_24736703的博客

记录博主的学习过程

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

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

 #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;
    }
}
#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;
    }
}

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

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文件里这问题就解决了!


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


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_24736703/article/details/51439853
文章标签: c++ error LNK 2019
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭