【发布时间】:2016-09-29 00:24:37
【问题描述】:
我看过其他类似的问题,但我并没有很好地理解答案。我收到此错误:
在函数main':
C:/Users/Danny/ClionProjects/LinkedList/main.cpp:9: undefined reference tolinkList::linkList()'
collect2.exe:错误:ld 返回 1 个退出状态
linkList.cpp:
#include <iostream>
#include <cstdlib>
#include "linkList.h"
using namespace std;
linkList::linkList()
{
head = NULL;
follow = NULL;
trail = NULL;
}
void linkList::addNode(int dataAdd)
{
nodePtr n = new node;
n->next = NULL;
n->data = dataAdd;
if (head != NULL)
{
follow = head;
while (follow->next != NULL)
{
follow = follow->next;
}
}
else
{
head = n;
}
}
void linkList::deleteNode(int nodeDel)
{
nodePtr delPtr = NULL;
follow = head;
trail = head;
while(follow != NULL)
{
trail = follow;
follow = follow->next;
if (follow->data == nodeDel)
{
delPtr = follow;
follow = follow->next;
trail->next = follow;
delete delPtr;
}
if(follow == NULL)
{
cout << delPtr << " was not in list\n";
delete delPtr; // since we did not use delPtr we want to delete it to make sure it doesnt take up memory
}
}
}
void linkList::printList()
{
follow = head;
while(follow != NULL)
{
cout << follow->data << endl;
follow = follow->next;
}
}
LinkList.h:
struct node {
int data;
node* next;
};
typedef struct node* nodePtr;
class linkList
{ // the linkList will be composed of nodes
private:
nodePtr head;
nodePtr follow;
nodePtr trail;
public:
linkList();
void addNode(int dataAdd);
void deleteNode(int dataDel);
void printList();
};
main.cpp:
#include <cstdlib>
#include "linkList.h"
using namespace std;
int main() {
linkList myList;
return 0;
}
我知道这与我的文件链接方式有关,当我在我的主文件中将 #include linkList.h 更改为 #include linkList.cpp 时它可以正常工作,为什么会这样?我有另一个类似的程序,它是一个二叉搜索树,它可以完美地工作并且具有基本相同的设置类型。所以我的问题是如何解决它?为什么会这样?
【问题讨论】: