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

标签 c++ class oop struct

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

出现的错误是:

assigning to 'struct _stack *' (aka '_stack *') from incompatible type 'struct _stack *' (aka 'Digit::_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();
};

最佳答案

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

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

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

关于c++ - struct _stack* 和 Digit::struct_stack* 中的一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55277157/

相关文章:

c++ - 将结构类型的地址传递给 boost::thread?

java - java "... "运算符做什么(在类构造函数中找到)

javascript - 在完全淡出之前淡入某个类

c# - 游戏项目类层次结构的建议

c++ - 线程内队列的临界区

c++ - 与线程相关的事件对象设计问题(c++ boost)

java - 如何正确从 Java Map 检索数据?

php - 将多个参数传递给 PHP 中的方法

c++ - 如何将表(数字列表)从 Lua 传递到 C 并访问它

c++ - C++ 标准是否解决了概念 "TYPE"?