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

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

本文关键字:结构 C++ 基类 引用 类型 elem 成员      更新时间:2024-04-28

此代码有什么问题?我真的不知道。我是初学者。对不起我的英语。


#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;

在此处输入图像描述


错误图像,

lista的类型为elem**:指向指针的指针。

->运算符将一个级别的指针延迟。因此lista->poprz与(*lista(.appz.相同

在这种情况下,您需要取消引用两次:(*lista)->poprz

注意:我没有试图理解为什么lista是指向指针的指针。它可能只是你想要elem*