I want an array of pointers in a class. This is the code I tried. But I get an error.
class Msg{
public:
char *msg[2]={
"one",
"two",
"three"
};
//there are some other methods I want to write.....
};
void main(){
Msg msg;
// i want to print msg[] here using a for loop
}
But it does not compile and shows an error in class and I also want to know how to access array of pointer which is a class member. Please correct the syntax if I am wrong.
edit:[i want to do]
i have around 12 fixed messages, which are displayed according to situation, i set a enum to get correct index like.
enum{
size,
area,
volume,
//etc
};
class Msg have a functionputMsg(int index) which cout required msg when i pass a enum.
if i pass area it will put a msg like "the area calculated by your equation is : "
is there any better way to do this type of messaging.
void mainisn't C++. You're also better off usingstd::stringand either using C++11 (with it's in-class member initializers), or member initializers in the constructor. You also assign three things to an array of size 2.*msg[2]should be*msg[3]