将数组传递给类的未声明大小的数组成员
Pass an array to an array member of undeclared size of a class
我想要一个数组,它的值从main声明为类。这是示例代码。
class test{
public:
const double arr[];
};
int main(){
test t;
t.arr[] = {1, 2};
return 0;
}
当我尝试在main中初始化时,它会给我一个错误错误:意外表达式
但如果我在main中删除t.arr[]
,它编译得很好。
const double arr[];
-可变长度数组?在C++中无效t.arr[]
-无效语法(没有参数的operator[]
调用?),arr
也是const
,不能分配给任何数组
但你可以进行聚合初始化:
class test {
public:
const double arr[2]; // fixed size
};
int main() {
test t = {{1, 2}}; // not an assignment
return 0;
}
相关文章:
- 如果我在相应 char 数组的声明中为其提供额外的元素,是否会自动设置 NULL?
- 在C++中用字符串数组拆分声明和赋值
- 为什么我的 2D 数组未定义?
- 结构数组 - 未引用的局部变量
- 具有空构造函数使数组未初始化会使计算速度变慢
- C 数组未从文件中获取正确的输入
- 使用指标后数组未修改
- 对于循环和并行数组:未声明的标识符
- 在数组中声明的字符串扫描用户输入
- C++ 成员数组未初始化
- 在头文件中的函数原型中获取指针数组:函数声明为 void
- 数组未加载 - "index out of range in objectAtIndex()"
- 数组未在此范围内声明
- C 数组未申报的标识符错误
- 二维数组未正确初始化
- 如何获取我在 2D 数组中声明和初始化的所有值的总和
- 动态字符数组未提供正确的字符串长度
- 字符* 数组出现"multiple definitions"错误,但无符号字符数组未显示错误
- 结构的类数组未正确初始化
- 未在范围 C++ [数组] 中声明