0

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.

3
  • 2
    void main isn't C++. You're also better off using std::string and 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. Commented Aug 13, 2012 at 16:27
  • What is the precise error message? Please cut-and-paste it, do not try to summarize it. Commented Aug 13, 2012 at 16:28
  • 1
    Is the error: "too many initializers" ? *msg[2] should be *msg[3] Commented Aug 13, 2012 at 16:30

3 Answers 3

6

Try this:

class Msg{
    public:

      // Can not initialize objects in the declaration part.
      // So just provide the declaration part.
      static char const *msg[];

      // Note: You had the completely wrong size for the array.
      //       Best to leave the size blank and let the compiler deduce it.

      // Note: The type of a string literal is 'char const*`

      // Note: I have made it static so there is only one copy.
      //       Since the strings are literals this should not affect usage
      //       But if you wanted one per class you need another technique.


    //there are some other method i want to write.....
};

// Now we can provide a definition.
// Note: I still leave the size blank and let the compiler deduce the size.
      char const* Msg::msg[]={
                   "one",
                   "two",
                    "three"
                 };  


// Note: main() must return an int.    
int  main(){
     Msg msg;
//  i want to print msg[] here using a for loop


    // The basic way
    for(std::size_t loop = 0;loop < sizeof(Msg::msg)/sizeof(Msg::msg[0]); ++loop)
    {
        std::cout << Msg::msg[loop] << "\n";
    }

    // Same as above but using C++11
    for(auto loop = std::begin(Msg::msg); loop != std::end(Msg::msg);++loop)
    {
        std::cout << *loop << "\n";
    }

    // using algorithms:
    std::copy(std::begin(Msg::msg), std::end(Msg::msg), std::ostream_iterator<char *const>(std::cout, "\n"));



     // Note: You don't actually need a return statement in main().
     //       If you don't explicitly provide one then 0 is returned.

}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks..now i got it . but i also don't know how to it in for loop in main. its first time i try like this and also not read any where in book. i know i can use . operator for normal variable but how to deal with it.
2

This has little to do with that it's an array of pointers (BTW not a particularly good thing to use, consider std::vector<std::string>) but with the way you're trying to initialise msg. This is a (nonstatic) member variable, so you have to initialise it in your class' constructor, not at the place it's declared.

class Msg{
    public:
      char *msg[3];

      Msg()
        : msg{ "one"
             , "two"
             , "three" }
      {}

//there are some other method i want to write.....
 };

4 Comments

I'm not 100% sure about C++03, but in 11 at least, using ({}) instead of {} is wrong.
I'm not sure either – it's nothing I would normally write, ever... but yeah, it's at any rate better without parens in this case, I suppose.
This is C++11. There's no simple way to do this in C++03; the best solution would be to wrap it in a struct, and copy it. (
The best solution is to make it static const as explained by Loki Astari: this way, nothing needs to be copied or constructed at all.
0

I suspect but I can't say for sure (you have not posted the actual error) that you are getting a "too many initializers error".

Change *msg[2] to *msg[3]. OR leave it BLANK. []

Or remove "three".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.