【发布时间】:2021-10-25 11:10:53
【问题描述】:
#include<iostream>
using namespace std;
class node
{
public:
int data;
node *next;
}*head;
class linkedlist
{
private:
node *first;
public:
linkedlist(){first=NULL;}
linkedlist(int A[], int n);
~linkedlist();
};
void linkedlist :: display(){
node *p = first;
while(p!=0){
cout<<p->data<<" ";
p=p->next;
}
}
int main(){
int A[]={2, 3, 4, 5, 6};
linkedlist l(A,5);
l.display();
}
我收到此错误:
C:\Users\DELL\AppData\Local\Temp\ccEQYgIh.o:ll_using_class.cpp:(.text+0x98): 未定义引用 linkedlist::linkedlist(int*, int)' C:\Users\DELL\AppData\Local\Temp\ccEQYgIh.o:ll_using_class.cpp:(.text+0xaf): undefined reference to linkedlist::~linkedlist()'
C:\Users\DELL\AppData\Local\Temp\ccEQYgIh.o:ll_using_class.cpp:(.text+0xc2): undefined reference to `linkedlist::~linkedlist()'
collect2.exe:错误:ld 返回 1 个退出状态
【问题讨论】:
-
这不是我得到的错误。我得到 "
error: no declaration matches 'void linkedlist::display()'" 但是如果我通过在类定义中添加void display();来解决这个问题,我会得到和你一样的错误,因为你没有实现构造函数linkedlist(int A[], int n);或析构函数 @ 987654326@ -
您编写了一个析构函数,但您没有提供该析构函数的定义(实现)。
-
你已经声明并使用了
linkedlist::linkedlist(int*, int),但你还没有定义它。 -
是的,我现在都实现了。谢谢