我在编写链接列表模板时遇到问题。它编译但我得到这些错误:
1> main.obj:错误LNK2019:未解析的外部符号" public:__ thishisall LinkedList ::〜LinkedList(void)"函数_main
中引用了(?? 1?$ LinkedList @ H @@ QAE @ XZ)1> main.obj:错误LNK2019:未解析的外部符号" public:void __thiscall LinkedList :: deleteNode(int)"函数_main
中引用了(?deleteNode @?$ LinkedList @ H @@ QAEXH @ Z)1> main.obj:错误LNK2019:未解析的外部符号" public:void __thiscall LinkedList :: displayList(void)" (?displayList @?$ LinkedList @ H @@ QAEXXZ)在函数_main
中引用1> main.obj:错误LNK2019:未解析的外部符号" public:void __thiscall LinkedList :: insertNode(int)" (?insertNode @?$ LinkedList @ H @@ QAEXH @ Z)在函数_main中引用
1> c:\ users \ pr0hz \ documents \ visual studio 2010 \ Projects \ ListTemplate \ Debug \ ListTemplate.exe:致命错误LNK1120:4个未解析的外部
任何帮助都会非常感激,因为我在过去几个小时里一直盯着我的屏幕试图解决这个问题。这是我的代码:
的main.cpp
#include <iostream>
#include <conio.h>
#include "LinkedList.h"
using namespace std;
int main()
{
LinkedList<int> list;
list.insertNode(5);
list.insertNode(7);
list.insertNode(1);
list.insertNode(19);
list.insertNode(16);
list.insertNode(22);
cout << "\nDisplay the list: \n";
list.displayList();
cout << endl;
list.deleteNode(16);
cout << "\nDisplay node with 16 deleted: \n";
list.displayList();
cout << endl;
LinkedList<int> list2 = list;
cout << "\nDisplay List 2: \n";
list2.displayList();
cout << endl;
getch();
return 0;
}
LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
using namespace std;
template <class T>
class LinkedList
{
private:
struct ListTemplate
{
T value;
ListTemplate *next;
};
ListTemplate *head;
public:
//Constructor
LinkedList()
{ head = NULL; }
//Destructor
~LinkedList();
//Copy Constructor
LinkedList(ListTemplate &obj)
{
ListTemplate *nodePtr = &obj; //Move through the list
ListTemplate *newNode = new ListTemplate;
if(!head)
return;
else
{
newNode = head;
while(nodePtr->next)
{
nodePtr = nodePtr->next;
newNode->next = nodePtr;
}
}
}
void appendNode(T);
void insertNode(T);
void deleteNode(T);
void displayList();
};
#endif
LinkedList.cpp
#include "LinkedList.h"
#include <iostream>
using namespace std;
template <class T>
void LinkedList<T>::appendNode(T newValue)
{
ListTemplate *newNode; //Point to the new node
ListTemplate *nodePtr; //Move through the list
//Allocate new node and store num there
newNode = new ListTemplate;
newNode->value = num;
newNode->next - NULL;
if(!head)
head = newNode;
else
{
//Initialize the nodePtr as head to move through the list
nodePtr = head;
//While there is still a node, we move through it until there is no more
while (nodePtr->next)
nodePtr = nodePtr->next;
//Append the node
nodePtr->next = newNode;
}
}
template <class T>
void LinkedList<T>::insertNode(T newValue)
{
ListTemplate *newNode; //New Node
ListTemplate *nodePtr; //Move through the list
ListTemplate *previousNode = NULL; //The previous node
//Allocate new node and store num
newNode = new ListTemplate;
newNode->value = num;
//If no node, new node is head
if (!head)
{
head = newNode;
newNode->next = NULL;
}
else //Insert node where it needs to be
{
nodePtr = head; //Beginning position
previousNode = NULL;
//Skip nodes that are less than newNode
while(nodePtr != NULL && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
//If newNode is the 1st in the list, insert it in slot 1
if (previousNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else //Insert after previous node
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
template <class T>
void LinkedList<T>::deleteNode(T searchNum)
{
ListTemplate *nodePtr;
ListTemplate *previousNode;
//If the list is empty, do nothing
if(!head)
return;
//See if the first node is the one
if(head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else //Find the node to delete
{
nodePtr = head;
//Skip the nodes not equal to num
while (nodePtr != NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
//Delete node and link previous node to the one of the one being deleted
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}
template <class T>
LinkedList<T>::~LinkedList()
{
ListTemplate *nodePtr;
ListTemplate *nextNode;
nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
template <class T>
void LinkedList<T>::displayList()
{
ListTemplate *nodePtr;
nodePtr = head;
while(nodePtr)
{
cout << nodePtr->value << " ";
nodePtr = nodePtr->next;
}
}
再次感谢您的帮助!
答案 0 :(得分:0)
您不应该在源文件中实现模板功能 见这里 - Why can templates only be implemented in the header file?
尝试将所有模板函数移动到头文件(LinkedList.h)中,然后重新编译
答案 1 :(得分:0)
不要将LinkedList分成.h和.cpp。链接器缺少你放入LinkedList.cpp的东西。