C++ Class 内部结构动态分配在它的方法
C++ Class internal struct dynamic allocation at its method
我在 class 中分配 (::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);
^
*/
感谢关注
嗯,老实说这是一个悲伤的疲劳事件。
如果他回答,我愿意接受@Passer By。
答案很简单@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;
....;
};
我仍然认为编译器错误有点误导
我在 class 中分配 (::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);
^
*/
感谢关注
嗯,老实说这是一个悲伤的疲劳事件。 如果他回答,我愿意接受@Passer By。
答案很简单@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;
....;
};
我仍然认为编译器错误有点误导