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();
};
编译器将struct _stack
中的struct _stack
和class 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();
};