array of pointer in c++ class

时间:2022-09-01 11:27:33

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.

我有大约12条固定的消息,根据情况显示,我设置一个枚举,以得到正确的索引。

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.

class Msg有一个functionputMsg(int index),当我传递枚举时,cout需要msg。如果我通过区域,它将输入一个msg,如“你的等式计算的区域是:”是否有更好的方法来做这种类型的消息传递。

3 个解决方案

#1


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.

}

#2


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.

这与它是一个指针数组(BTW不是一个特别好的东西,考虑std :: vector )有关,但与你尝试初始化msg的方式有关。这是一个(非静态)成员变量,因此您必须在类的构造函数中初始化它,而不是在它声明的位置。

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

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

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

#3


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. []

将* msg [2]更改为* msg [3]。或者留下BLANK。 []

Or remove "three".

或删除“三”。

#1


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.

}

#2


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.

这与它是一个指针数组(BTW不是一个特别好的东西,考虑std :: vector )有关,但与你尝试初始化msg的方式有关。这是一个(非静态)成员变量,因此您必须在类的构造函数中初始化它,而不是在它声明的位置。

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

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

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

#3


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. []

将* msg [2]更改为* msg [3]。或者留下BLANK。 []

Or remove "three".

或删除“三”。