C++ c+中的指针数组+;班

C++ c+中的指针数组+;班,c++,arrays,class,pointers,C++,Arrays,Class,Pointers,我想要一个类中的指针数组。 这是我试过的代码。但是我犯了一个错误 class Msg{ public: char *msg[2]={ "one", "two", "three" }; //there are some other methods I want to write..... }; void main(){

我想要一个类中的指针数组。 这是我试过的代码。但是我犯了一个错误

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
}
但它并没有编译,并且在类中显示了一个错误,我还想知道如何访问作为类成员的指针数组。 如果我错了,请纠正语法

edit:[我想做]

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

enum{
    size,
    area,
    volume,
    //etc 
})

class Msg
有一个函数
putMsg(int index)
,当我传递枚举时,它
cout
需要Msg。 如果我通过
area
,它将显示一条消息,如“通过公式计算的面积为:
有没有更好的方法来发送这种类型的消息 STD::vector < /代码>),但是用你试图初始化<代码> MSG的方式。这是一个(非静态)成员变量,因此必须在类的构造函数中初始化它,而不是在声明它的位置

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

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

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

我怀疑,但我不能肯定(你没有公布实际错误)你得到了“太多的初始化错误”

*msg[2]
更改为
*msg[3]
。或者让它空白<代码>[]

或者删除“三个”。

尝试以下操作:

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.

}
class Msg{
公众:
//无法初始化声明部分中的对象。
//所以只需提供声明部分。
静态字符常量*msg[];
//注意:数组的大小完全错误。
//最好将大小留空,让编译器推断出来。
//注意:字符串文字的类型是“char const”*`
//注意:我已将其设置为静态,因此只有一个副本。
//由于字符串是文本,因此不应影响使用
//但是如果你想每堂课上一个,你需要另一种技巧。
//我还想写一些其他的方法。。。。。
};
//现在我们可以提供一个定义。
//注意:我仍然将大小留空,让编译器推断大小。
char const*Msg::Msg[]={
“一个”,
“两个”,
“三个”
};  
//注意:main()必须返回一个int。
int main(){
味精;
//我想在这里使用for循环打印msg[]
//基本途径
对于(std::size_t loop=0;loopstd::难道你也最好使用
std::string
,或者使用C++11(带有类中的成员初始值设定项),或者在构造函数中使用成员初始值设定项。你还可以为大小为2的数组分配三件事。确切的错误消息是什么?请剪切并粘贴它,不要尝试对其进行总结。错误是:“初始值设定项太多”?
*msg[2]
应该是
*msg[3]
我不是100%确定C++03,但至少在11种情况下,使用
({})
而不是
{}
是错误的。我也不确定——这不是我通常会写的东西,永远……但我想,在这种情况下,没有parens无论如何都会更好。这是C++11。在C++03中,没有简单的方法可以做到这一点;最好的解决方案是将它包装在一个结构中,然后复制它。(最好的解决方案是让它成为
静态常量,正如Loki Astari所解释的那样:这样,就不需要复制或构造任何东西了。谢谢。现在我得到了。但我也不知道如何在主循环中使用它。这是我第一次这样尝试,也没有读过书中的任何地方。我知道我可以对正态变量使用.operator,但如何使用它。)处理它。