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 _stack
和 struct _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();
};
关于c++ - struct _stack* 和 Digit::struct_stack* 中的一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55277157/