I wish to define an array in a class, and set the variables of the class to the elements of the array. The implementation below results in segmentation fault:
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];
}
How should I fix it?
Quickest way is to change
char MAP[];tochar MAP[3];There are other interesting things in the code.
1) It does not compile as given (you never define what a percent is).
2) What happens if someone sends an “int a” to your set function that is outside of the range of your map? (IE: 56 instead of 0, 1, or 2)?