char数组作为类成员变量 [英] char arrays as class member variables

查看:110
本文介绍了char数组作为类成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图像这样声明一个char [] []:


在标题中我有:


i am trying to declare a char[][] like this:

Granery::foodNames[5][100] = {"Meat","Fruit","Wheat","Vegetables","Dairy"};

while in the header i have:


展开 | 选择 | Wrap | 行号

推荐答案

为什么在C ++中使用字符数组?这就是std :: string的用途。
Why are you using character arrays in C++? That''s what std::string was made for.


ok..good point但我仍然遇到与字符串相同的问题[]


Granery :: foodNames = {" Meat,Fruit,Wheat,Vegetables,Dairy};


在Granery.cpp默认的构造函数给了我这个:


[C ++错误] Granery.cpp(7):E2188表达式语法


i意识到这可能听起来像一个愚蠢的问题,但由于某些原因我很难用这个


再次感谢

ken
ok..good point but i still have the same problem with the string[]

Granery::foodNames = {"Meat","Fruit","Wheat","Vegetables","Dairy"};

declared in the Granery.cpp default contructor gives me this:

[C++ Error] Granery.cpp(7): E2188 Expression syntax


i realize this may sound like a dumb question, but for some reason im having a hard time with this


thanks again
ken


您的基本问题是,当您尝试初始化静态变量时,您没有正确声明它。在类外部初始化静态变量时,必须包含该类型。在您的情况下,您需要在名称前添加char。您还需要使char不变,因为您正在将数组初始化为常量字符串。


对于你写的字符串:
Your basic problem is that when you try to initialize the static variable, you are not declaring it correctly. When inititializing a static variable outside the class, you have to include the type. In your case, you need to add char before the name. You also need to make the char constant since you''re initializing the array to constant strings.

For strings you woud write:
展开 | 选择 | Wrap | 行号


这篇关于char数组作为类成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆