[英]Pass an array to an array member of undeclared size of a class
I want to have an array whose values be declared from main to a class.我想要一个数组,其值从 main 声明为一个类。 Here's the sample code.
这是示例代码。
class test{
public:
const double arr[];
};
int main(){
test t;
t.arr[] = {1, 2};
return 0;
}
When I try to intialize in the main it gives me an error error:unexpected expression.当我尝试在 main 中初始化时,它给了我一个错误error:unexpected expression。
But if I remove the t.arr[]
in main, it compiles fine.但是如果我删除
t.arr[]
中的t.arr[]
,它编译得很好。
const double arr[];
- variable length array? t.arr[]
- invalid syntax ( operator[]
call with no arguments?), arr
is also const
and you can't assign to any arrays. t.arr[]
- 无效语法(不带参数的operator[]
调用?), arr
也是const
并且您不能分配给任何数组。 But you can do aggregate intialization :但是你可以做聚合初始化:
class test {
public:
const double arr[2]; // fixed size
};
int main() {
test t = {{1, 2}}; // not an assignment
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.