阅读
How to initialize an array in C之后,特别是:
Don’t overlook the obvious solution, though:
int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
我试过这样的事情:
#include
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);
}
我得到:
..\src\Something.cpp: In constructor 'Something::Something()':
..\src\Something.cpp:10:48: error: cannot convert '' to 'int' in assignment
在这种情况下显而易见的并不是那么明显.我真的希望我的阵列的启动也更加动态.
我累了:
private:
int * myArray;
public:
Something() {
myArray = new int [10];
myArray = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
}
对我来说这看起来很时髦,对编译器来说也是如此:
..\src\Something.cpp: In constructor 'Something::Something()':
..\src\Something.cpp:11:44: error: cannot convert '' to 'int*' in assignment
这也行不通:
private:
int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
有:
..\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
我一直做得很好,学习什么不起作用,但不是很好学习什么工作.
那么,我如何在类中使用数组的初始化列表{value,value,value}?
我一直试图弄清楚如何做一段时间并且非常困难,我有很多这样的列表我需要为我的应用程序制作.