我希望在一个类中定义一个数组,并将类的变量设置为数组的元素。下面的实现会导致分段错误:
class Grade {
char MAP[];
char *letter;
public:
Grade();
~Grade();
void set(int);
};
Grade::Grade(){
letter = new char;
*letter = '\0';
MAP[0] = 'A';
MAP[1] = 'B';
MAP[2] = 'C'; // result in segmentation fault
MAP = { 'A', 'B', 'C'}; // result in segmentation fault
}
Grade::~Grade(){
delete letter;
delete percent;
}
void Grade::set(int a){
*letter = MAP[a];
}
我应该如何解决它?