简体   繁体   English

成员引用基类型“elem *”不是结构或联合 C++

[英]Member reference base type 'elem *' is not a structure or union C++

What's wrong with this code?这段代码有什么问题? I really don't know.我真的不知道。 I'm beginner.我是初学者。 Sorry for my english.对不起我的英语不好。


#include <iostream>
using namespace std;

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

class ListaDwukierunkowa
{
protected:
    elem **lista;

public:
    ListaDwukierunkowa(elem** lista)
    {
        this->lista = lista;
    }

    void dodaj_elem(int do_dodania)
    {
        if (*lista == nullptr)
        {
            *lista = new elem;
            (*lista)->wartosc = do_dodania;
            (*lista)->nast = nullptr;
            (*lista)->poprz = nullptr;
        }
        else
        {
            elem *temp = *lista;
            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 (lista == nullptr)
        {
            cout << "Lista jest pusta" << endl;
        }
        while (lista != nullptr)
        {
            cout << "poprz: " <<  lista->poprz << " | ten: " << lista << " | war:" << lista->wartosc << " | nast:" << lista->nast << endl;
            lista = lista->nast;
        }
    }

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

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

        if (temp->poprz)
            temp->poprz->nast = temp->nast;
        else
            *lista = temp->nast;

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

        delete temp;
    }

    int liczba_elem()
    {
        int liczba = 0;
        while (lista != nullptr)
        {
            liczba += 1;
            lista = lista->nast;
        }
        return liczba;
    }

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

    bool czy_zawiera(int x)
    {
        bool czy = false;
        while (lista != nullptr)
        {
            if (lista->wartosc == x)
            {
                czy = true;
            }
            lista = lista->nast;
        }
        return czy;
    }

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




int main()
{
    ListaDwukierunkowa lista;

    lista.dodaj_elem(1);
    lista.wyswietl_elem();

//    dodaj_elem(&wsk, 12);
//    dodaj_elem(&wsk, 9);
//    dodaj_elem(&wsk, 8);
//    dodaj_elem(&wsk, 9);
//    dodaj_elem(&wsk, 9);
//    wyswietl_elem(wsk);
//    cout << liczba_elem(wsk) << endl;
//    cout << liczba_elem_o_war(wsk, 9) << endl;
//    cout << boolalpha << czy_zawiera(wsk, 12) << endl;
//    usun_elem(&wsk, 1);
//    wyswietl_elem(wsk);
//    zwolnij_liste(&wsk);

}
error: request for member 'poprz' in '*((ListaDwukierunkowa*)this)->ListaDwukierunkowa::lista', which is of pointer type 'elem*' (maybe you meant to use '->' ?)
             cout << "poprz: " <<  lista->poprz << " | ten: " << lista << " | war:" << lista->wartosc << " | nast:" << lista->nast << endl;

enter image description here在此处输入图像描述


image of errors,错误图像,

lista is of type elem** : a pointer to a pointer. listaelem**类型:指向指针的指针。

The -> operator defererences one level of pointers. ->运算符延迟一级指针。 So lista->poprz is the same as (*lista).poprz.所以lista->poprz和 (*lista).poprz 是一样的。

In this case you need to dereference twice: (*lista)->poprz .在这种情况下,您需要取消引用两次: (*lista)->poprz

Note: I have not tried to understand why lista is a pointer to a pointer.注意:我没有试图理解为什么lista是指向指针的指针。 It could be that you simply want elem* instead.可能您只是想要elem*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM