如何初始化C ++对象的数组。 [英] How to initialize an array in C++ objects
问题描述
看完后如何初始化用C 一个数组,在特别是:
不要忽略了明显的解决方案,虽然:
INT myArray的[10] = {5,5,5,5,5,5,5,5,5,5};
块引用>我想是这样的:
的#include<&iostream的GT;类的东西{
私人的:INT myArray的[10];上市:东西(){
myArray的[10] = {5,5,5,5,5,5,5,5,5,5};
}INT ShowThingy(INT什么){
返回myArray的[什么];
}〜事(){}
};诠释主(){
什么事情;
的std :: CERR<< Thing.ShowThingy(3);
}和我得到:
.. \\ SRC \\ Something.cpp:在构造'的东西::东西():
.. \\ SRC \\ Something.cpp:10:48:错误:无法转换'<大括号内的初始化列表>'在分配'廉政'在这种情况下,显然是不那么明显。我真的想我的数组的开始要和更有活力。
我累了:
私人:
为int * myArray的;上市:
东西(){
myArray的=新INT [10];
myArray的= {5,5,5,5,5,5,5,5,5,5};
}这看起来时髦给我,所以编译器:
.. \\ SRC \\ Something.cpp:在构造'的东西::东西():
.. \\ SRC \\ Something.cpp:11:44:错误:无法转换'<大括号内的初始化列表>'在分配'诠释*'这也没有工作:
私人:
INT myArray的[10] = {5,5,5,5,5,5,5,5,5,5};与
.. \\ SRC \\ Something.cpp:6:20:错误:一个大括号内的初始化在这里不允许使用前'{'令牌
.. \\ SRC \\ Something.cpp:6:51:对不起,没有实现:非静态数据成员初始化
.. \\ SRC \\ Something.cpp:6:51:错误:'constexpr非整数类型需要在类的静态数据成员的初始化myArray的我一直在做非常好,学什么不工作,但不是那么好学的东西确实工作。
所以,我怎么用初始化列表{价值,价值,价值}一类里的数组?
我一直在试图找出如何有一段时间了做到这一点,我非常坚持,我有很多这些类型的列表我需要为我的应用程序。
解决方案您需要初始化在构造函数初始化列表中的数组
的#include<&iostream的GT;类的东西{
私人的:INT myArray的[10];上市:东西()
:myArray的{5,5,5,5,5,5,5,5,5,5}
{
}INT ShowThingy(INT什么){
返回myArray的[什么];
}〜事(){}
};诠释主(){
什么事情;
的std :: CERR<< Thing.ShowThingy(3);
}
.. \\ SRC \\ Something.cpp:6:51:对不起,没有实现:非静态数据成员初始化
块引用>C ++ 11还增加了支持非静态成员变量的初始化内联,但上面的错误消息的状态,你的编译器还没有实现这个呢。
After reading How to initialize an array in C, in particular:
Don't overlook the obvious solution, though:
int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
I tried something like this:
#include <iostream> class Something { private: int myArray[10]; public: Something() { myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }; } int ShowThingy(int what) { return myArray[what]; } ~Something() {} }; int main () { Something Thing; std::cerr << Thing.ShowThingy(3); }
And I get:
..\src\Something.cpp: In constructor 'Something::Something()': ..\src\Something.cpp:10:48: error: cannot convert '<brace-enclosed initializer list>' to 'int' in assignment
The obvious in this case is not so obvious. I really would like the initiation of my array to be more dynamic as well.
I tired:
private: int * myArray; public: Something() { myArray = new int [10]; myArray = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }; }
This looked funky to me to, and so to the compiler:
..\src\Something.cpp: In constructor 'Something::Something()': ..\src\Something.cpp:11:44: error: cannot convert '<brace-enclosed initializer list>' to 'int*' in assignment
This also did not work:
private: int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
with:
..\src\Something.cpp:6:20: error: a brace-enclosed initializer is not allowed here before '{' token ..\src\Something.cpp:6:51: sorry, unimplemented: non-static data member initializers ..\src\Something.cpp:6:51: error: 'constexpr' needed for in-class initialization of static data member 'myArray' of non-integral type
I have been doing really good and learning what does not work, but not so good learning what does work.
So, how do I used initialization lists {value, value, value} for an array inside a class?
I have been trying to figure out how to do this for some time now and am very stuck, I have a number of these kinds of lists I need to make for my app.
解决方案You need to initialize the array in the constructor initialization list
#include <iostream> class Something { private: int myArray[10]; public: Something() : myArray { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 } { } int ShowThingy(int what) { return myArray[what]; } ~Something() {} }; int main () { Something Thing; std::cerr << Thing.ShowThingy(3); }
..\src\Something.cpp:6:51: sorry, unimplemented: non-static data member initializers
C++11 also adds supports for inline initialization of non-static member variables, but as the above error message states, your compiler has not implemented this yet.
这篇关于如何初始化C ++对象的数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!