You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++双向链表代码报Member reference base type 'elem *' is not a structure or union错误

问题排查与修复方案

报错原因

error: request for member 'poprz' in '((ListaDwukierunkowa)this)->ListaDwukierunkowa::lista', which is of pointer type 'elem*' (maybe you meant to use '->' ?)

上述报错的核心原因是:类的成员变量lista是elem**二级指针类型,不能直接用->访问elem结构体的成员,必须先解引用得到elem*类型后才能访问成员。

代码中存在的所有问题

  • 构造函数不匹配:你定义的构造函数要求传入elem**参数,但main函数中直接调用无参构造创建对象,C++不会自动生成匹配的无参构造,会直接编译报错。
  • 二级指针访问逻辑错误:所有遍历方法中直接用lista->成员的写法不符合二级指针的访问规则,必须先解引用。
  • 遍历修改头指针:所有遍历逻辑直接修改成员变量lista本身,遍历结束后链表头指针丢失,后续所有操作都会出错,遍历必须用临时变量保存头节点地址。
  • 空链表判断错误:判断链表为空应该判断*lista == nullptr,而不是lista == nullptr。

修正后的完整代码

#include <iostream>
using namespace std;

struct elem
{
    int wartosc;
    elem* nast;
    elem* poprz;
};

class ListaDwukierunkowa
{
protected:
    // 改为一级指针作为头结点即可,不需要二级指针降低理解成本
    elem* head;

public:
    // 补充无参构造函数
    ListaDwukierunkowa()
    {
        head = nullptr;
    }

    void dodaj_elem(int do_dodania)
    {
        if (head == nullptr)
        {
            head = new elem;
            head->wartosc = do_dodania;
            head->nast = nullptr;
            head->poprz = nullptr;
        }
        else
        {
            elem *temp = head;
            while (temp->nast != nullptr)
            {
                temp = temp->nast;
            }
            temp->nast = new elem;
            temp->nast->wartosc = do_dodania;
            temp->nast->nast = nullptr;
            temp->nast->poprz = temp;
        }
    }

    void wyswietl_elem()
    {
        cout << endl;
        if (head == nullptr)
        {
            cout << "Lista jest pusta" << endl;
            return;
        }
        // 用临时变量遍历,不修改原头指针
        elem* temp = head;
        while (temp != nullptr)
        {
            cout << "poprz: " <<  temp->poprz << " | ten: " << temp << " | war:" << temp->wartosc << " | nast:" << temp->nast << endl;
            temp = temp->nast;
        }
    }

    void usun_elem(int do_usun)
    {
        elem *temp = head;
        if (head == nullptr)
            return;

        while (temp->wartosc != do_usun)
        {
            temp = temp->nast;
            if (temp == nullptr)
                return;
        }

        if (temp->poprz)
            temp->poprz->nast = temp->nast;
        else
            head = temp->nast;

        if (temp->nast)
            temp->nast->poprz = temp->poprz;

        delete temp;
    }

    int liczba_elem()
    {
        int liczba = 0;
        elem* temp = head;
        while (temp != nullptr)
        {
            liczba += 1;
            temp = temp->nast;
        }
        return liczba;
    }

    int liczba_elem_o_war(int x)
    {
        int liczba = 0;
        elem* temp = head;
        while (temp != nullptr)
        {
            if (temp->wartosc == x)
            {
                liczba += 1;
            }
            temp = temp->nast;
        }
        return liczba;
    }

    bool czy_zawiera(int x)
    {
        bool czy = false;
        elem* temp = head;
        while (temp != nullptr)
        {
            if (temp->wartosc == x)
            {
                czy = true;
                // 找到目标后提前退出,优化执行效率
                break;
            }
            temp = temp->nast;
        }
        return czy;
    }

    void zwolnij_liste()
    {
        while (head != nullptr)
        {
            elem* nast = head->nast;
            delete head;
            head = nast;
        }
        head = nullptr;
    }

    // 补充析构函数自动释放内存,避免内存泄漏
    ~ListaDwukierunkowa()
    {
        zwolnij_liste();
    }
};

int main()
{
    ListaDwukierunkowa lista;

    lista.dodaj_elem(1);
    lista.dodaj_elem(12);
    lista.dodaj_elem(9);
    lista.dodaj_elem(8);
    lista.dodaj_elem(9);
    lista.dodaj_elem(9);
    lista.wyswietl_elem();
    cout << lista.liczba_elem() << endl;
    cout << lista.liczba_elem_o_war(9) << endl;
    cout << boolalpha << lista.czy_zawiera(12) << endl;
    lista.usun_elem(1);
    lista.wyswietl_elem();
    lista.zwolnij_liste();
    return 0;
}

内容的提问来源于stack exchange,提问作者Adam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 21:06:01