struct _stack *和Digit :: struct_stack *中的一些问题

问题描述 投票:2回答:1
void Digit::push(int value){

    struct _stack *next_field = new struct _stack;
    if (end == nullptr && start == nullptr){
        next_field->_next_ptr = nullptr; //in codebloks project. next_ptr = himself
        start = next_field;
    }
    else
        next_field->_next_ptr = end;
    next_field->_data = value;
    end = next_field;
}

显示的错误是:

从不兼容的类型'struct _stack *'(又名'Digit :: _ stack *')分配'struct _stack *'(又名'_stack *')

我该如何解决?

这是Header Digit类:

class Digit
{
    struct _stack *start = nullptr;
    struct _stack *end = nullptr;
    struct _stack *ptr_element = nullptr;

    struct _stack {
        _stack* _next_ptr = nullptr;
        int _data = 0;
    }_element;

public:
    Digit();
    void push(int);
    void pop();
};
c++ class oop struct
1个回答
1
投票

编译器将struct _stack中的struct _stackclass Digit视为两个不同的实体。

要解决此问题,请在声明指针之前在struct _stack中移动class Digit的定义。该类应如下所示:

class Digit
{
    struct _stack {
        _stack* _next_ptr = nullptr;
        int _data = 0;
    }_element;

    struct _stack *start = nullptr;
    struct _stack *end = nullptr;
    struct _stack *ptr_element = nullptr;   

public:
    Digit();
    void push(int);
    void pop();
};

See Demo

© www.soinside.com 2019 - 2024. All rights reserved.