我在分配(::operator new)类中声明的结构(在其方法上)时遇到了一些麻烦,因为它得到了一个不可读的错误:
error: cannot convert 'Automata::state*' to 'state*' in assignment我试过删除"Automata::“声明,放置"this->”和其他随机的东西,但没有成功。遵循示例代码;
#include <iostream>
#include <new>
class Automata{
public:
struct quintuple{
struct state *stateStr = NULL;
int stateSize = 0;
};
struct state{
struct symbol *symbolStr = NULL;
};
struct symbol{
char *state = NULL;
int stateSize = 0;
};
struct quintuple quintupleStr;
//Functions
void quintuple(int stateValidCounter);
};
void Automata::quintuple(int stateValidCounter){
this->quintupleStr.stateStr = (struct Automata::state *) ::operator new(sizeof(struct Automata::state) * stateValidCounter);
return;
}
int main(){
Automata automata;
automata.quintuple(5);
return 0;
}
/*
g++ -std=c++11 example.cpp -o example.out
example.cpp: In member function 'void Automata::quintuple(int)':
example.cpp:23:30: error: cannot convert 'Automata::state*' to 'state*' in assignment
this->quintupleStr.stateStr = (struct Automata::state *) ::operator new(sizeof(struct Automata::state) * stateValidCounter);
^
*/谢谢大家的关注。
发布于 2018-04-23 06:43:39
老实说,这是一件令人悲伤的劳累事件。如果他接电话的话,我愿意接受他的回答。
答案很简单,就像主部分的@Passer注释一样。
做代码
#include <iostream>
#include <new>
class Automata{
public:
struct quintuple{
struct state *stateStr = NULL;
int stateSize = 0;
};
struct state{....};
struct quintuple quintupleStr;
//Functions
void quintuple(int stateValidCounter);
};
void Automata::quintuple(int stateValidCounter){
this->quintupleStr.stateStr = (struct Automata::state *) ::operator new(sizeof(struct Automata::state) * stateValidCounter);
return;
}
int main(){
Automata automata;
automata.quintuple(5);
return 0;
}只需切换结构代码位置。
class Automata{
public:
struct state{....};
struct quintuple{
struct state *stateStr = NULL;
int stateSize = 0;
};
struct quintuple quintupleStr;
....;
};我仍然认为编译器错误有点误导性。
https://stackoverflow.com/questions/49974543
复制相似问题