【问题标题】:I am getting this error when i try to display my linkedlist [duplicate]当我尝试显示我的链表时出现此错误 [重复]
【发布时间】: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),但你还没有定义它。
  • 是的,我现在都实现了。谢谢

标签: c++ arrays class pointers


【解决方案1】:
  • 您已定义 void linkedlist::display(),但尚未在类定义中声明它。
  • 您已声明构造函数 linkedlist(int A[], int n),但尚未定义它。
  • 您已声明析构函数~linkedlist(),但尚未定义它。

其他说明:

  • 全局 node* head 毫无意义。
  • node 类型不需要在linkedlist 类之外可见。我建议通过在linkedlist 中将node 定义设为私有来隐藏它。
  • display() 成员函数不会更改 linkedlist 对象。制作这样的函数const

修复示例:

#include <iostream>
#include <utility> // std::exchange

class linkedlist {
private:
    struct node {     // made definition private
        int data;
        node *next;
    };

    node* first;
public:
    linkedlist();
    linkedlist(int A[], int n);
    linkedlist(const linkedlist&) = delete;            // copying not implemented
    linkedlist& operator=(const linkedlist&) = delete; // copying not implemented
    ~linkedlist();
    void add(int value); // a function to add a value to the linked list
    void display() const; // made const 
};

linkedlist::linkedlist() : first(nullptr) {}

linkedlist::linkedlist(int A[], int n) : first(nullptr) {
    for(size_t idx = 0; idx < n; ++idx) {
        add(A[idx]);                     // use the add() member function
    }
}

linkedlist::~linkedlist() {
    while(first) { // delete all nodes
        delete std::exchange(first, first->next);
    }
}

void linkedlist::add(int value) {
    // add a node by linking it in first
    first = new node{value, first};
}

void linkedlist::display() const {
    if(first) {
        std::cout << first->data;
        for(node* p = first->next; p; p = p->next) {
            std::cout << ' ' << p->data;
        }
    }
}

int main(){
    int A[]={2, 3, 4, 5, 6};
    linkedlist l(A, 5);
    l.display();
}

输出

6 5 4 3 2

【讨论】:

  • 哦,对了。我对我的代码进行了更改,请看一下。它现在工作......谢谢:)
  • @NoobCoder 不客气!在你得到答案后,请不要从根本上改变你的问题。问题+答案现在没有意义,因为这是一个问答网站,它不会帮助任何其他人在目前的状态。我将还原您所做的更改。尽管您确实遇到了一些问题,例如 for(int i=1;i&lt;n;i++) 跳过了第一个元素。数组从 C++ 中的元素 0 开始。
  • 哦,明白我以后不会这样做了。另外,我在第一个节点本身中添加第一个元素。
  • @NoobCoder 啊..我明白了。这是一种不寻常的做法。我错过了。 :)
  • 我刚开始学习 DSA xD
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-17
  • 1970-01-01
  • 2020-01-28
  • 2021-08-25
  • 2016-05-14
  • 2015-09-12
  • 2019-10-29
相关资源
最近更新 更多