我决定创建一个列表(出于练习的原因,所以不用担心重新发明轮子了。)

我收到了此LNK2019错误,我相信它的代码特定的becus,我似乎找不到解决它的方法(是的,我在这里随地就睡)

我发现问题被滥用,因为编译器无法识别方法的定义,但是我似乎找不到答案。
我正在使用MVS 2010,因此链接参数自动完成。这是我的代码。

list H:

#ifndef _DLIST
#define _DLIST

#include "Node.h"

template<class DataO>
class Dlist
{
private:
    /* Data Members */
             Node<DataO>* _ndHead;
             Node<DataO>* _ndTail;
    unsigned int   _nLength;

    /* Methods */
    void Delete();

public:
    /* Ctors && Dtors */
    Dlist();
    ~Dlist();

    /* Getters && Setters */
    int Length();

    /* Methods */
    void Add(DataO doData);
    void Clear();
    void RemoveAt(unsigned int nIndex);
    void Remove();
};

#endif

list Cpp:
// Dlist.cpp

#include "Dlist.h"

/* ------------------ Dlist Section ------------------ */
/* --- Constracturs && Destractur --- */
template<class DataO>
Dlist<DataO> :: Dlist()
{
    // Init data
    this->_ndHead = NULL;
    this->_ndTail = NULL;
    this->_nLength = 0;
}

template<class DataO>
Dlist<DataO> :: ~Dlist()
{
    // Check if we have nodes to delete
    if (this->_nLength > 0);
    {
        this->Clear();
    }
}

/* --- Methods -- */
template<class DataO>
int Dlist<DataO> :: Length()
{
    // Return the length
    return (this->Length);
}
template<class DataO>
void Dlist<DataO> :: Add(DataO doData)
{
// Check if head is null
if (this->_ndHead == NULL)
{
    // Create head
    this->_ndHead = new Node<DataO>(doData, NULL, NULL);
}
// Check if tail is null
else if (this->_ndTail == NULL)
{
    // Create tail
    this->_ndTail = new Node<DataO>(doData, NULL, this->_ndHead);

    // Set head's next
    this->_ndHead->SetNext(this->_ndTail);
}
// Create a new Node and replace the tail.
else
{
    // Create a new node.
    this->_ndTail->_ndNext = new Node<DataO>(doData, NULL, this->_ndTail);

    // Make the new node the tail
    this->_ndTail = this->_ndTail->_ndNext;
}
}

节点H:
    template<class DataO>
class Node
{
private:
    /* Data Members */
    DataO  _doData;
    Node<DataO>*  _ndPrev;
    Node<DataO>*  _ndNext;

    /* Methods */
    void Delete();

public:
    /* Ctors && Dtors */
    Node<DataO>(DataO doData);
    Node<DataO>(DataO doData, const Node& ndNext, const Node& ndPrev);
    ~Node();

    /* Getters && Setters */
    DataO&  GetData();
    void    SetData(DataO doData);

    const Node& GetNext();
          void  SetNext(const Node* ndNextNode);
    const Node& GetPrev();
          void  SetPrev(const Node* ndPrevNode);
};

节点Cpp:
// Node.cpp

#include "Node.h"

/* ------------------ Node Section ------------------ */
/* --- Constracturs --- */
template<class DataO>
Node<DataO> :: Node(DataO doData)
{
    // Init data
    this->SetData(doData);
    this->SetNext(NULL);
    this->SetPrev(NULL);
}

template<class DataO>
Node<DataO> :: Node(DataO doData, const Node& ndNext, const Node& ndPrev)
{
    // Init data
    this->SetData(doData);
    this->SetNext(ndNext);
    this->SetPrev(ndPrev);
}

/*
    -   Node destructor -
    -   Maneges the change of nodes.
    -   Changes the Previous node next node,
    -   to the current node next node.
*/
template<class DataO>
Node<DataO> :: ~Node()
{
    // Check that prev is not null
    if (this->GetPrev != NULL)
    {
        // Set prevs Next to current next
        this->GetPrev().SetNext(this->GetNext());
    }
}

/*
    -   GetData -
    -   Returns the data stored in the Node.
*/
template<class DataO>
DataO& Node<DataO> :: GetData()
{
    // Return the data
    return (this->_doData&);
}

/*
    -   SetData -
    -   Sets the data stored in the Node.
*/
template<class DataO>
void Node<DataO> :: SetData(DataO doData)
{
    // Sets data
    this->_doData = doData;
}

/*
    -   GetNext -
    -   Returns the next node in the list.
*/
template<class DataO>
const Node<DataO>& Node<DataO> :: GetNext()
{
    // Returns the next node in the list.
    return (this->_ndNext&);
}

/*
    -   SetNext -
    -   Set the next node in the list.
*/
template<class DataO>
void Node<DataO> :: SetNext(const Node* ndNextNode)
{
    // Set next node as given node
    this->_ndNext = ndNextNode;
}

/*
    -   GetPrev -
    -   Returns the previous node in the list.
*/
template<class DataO>
const Node<DataO>& Node<DataO> :: GetPrev()
{
    // Returns the next node in the list.
    return (this->_ndPrev&);
}

/*
    -   SetPrev -
    -   Set the previous node in the list.
*/
template<class DataO>
void Node<DataO> :: SetPrev(const Node* ndPrevNode)
{
    // Set next node as given node
    this->GetPrev = ndPrevNode;
}

最佳答案

发生这种情况是因为MSVC编译器不支持外部模板。我知道您的想法,这是正确的方法,但不适用于大多数编译器。这是因为编译器需要访问整个模板定义(而不仅仅是签名)才能为模板的每个实例生成代码。您有2种选择可以使其正常工作:

  • 定义头文件中的所有方法(就像大多数Boost库一样)
  • 显式实例化您的模板类以获取预定义类型(这样,您的类将不会是通用的)

  • 查看详细说明here

    关于c++ - LNK2019错误 Unresolved external symbol ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12536106/

    10-09 03:09