struct S {
double arr[1];
S(double arr[1]) : arr(arr) {}
};
int main(void) {
double arr[1] = {1.2};
S p(arr);
return 0;
}
Hi, this is an extracted problem I encountered in my code.
嗨,这是我在代码中遇到的一个提取问题。
Do you know why this code won't compile?
你知道为什么这段代码不能编译吗?
main.cpp: In constructor ‘S::S(double*)’:
main.cpp:26:28: error: incompatible types in assignment of ‘double*’ to ‘double [1]’
S(double arr[1]) : arr(arr) {}
I am using g++
, compiling and running with
我正在使用g ++,编译和运行
g++ -std=c++17 main.cpp kdtree.h kdtree.cpp && ./a.out
4 个解决方案
#1
7
Arrays can't be copied.
数组无法复制。
int a[3];
int b[3];
a = b; // illegal
Further, when you pass an array to a function, its name decays to a pointer, so S(double arr[1])
is equivalent to S(double* arr)
. Once you're inside the function, you have to copy the individual elements, so you also need the size of the array:
此外,当您将数组传递给函数时,其名称会衰减为指针,因此S(double arr [1])等效于S(double * arr)。一旦进入函数内部,就必须复制单个元素,因此您还需要数组的大小:
S(double *x, std::size_t sz) {
std::copy_n(x, sz, arr);
}
You can omit the size if you write the template as a function:
如果将模板编写为函数,则可以省略大小:
template <std::size_t sz)
S(double (&x)[sz]) {
std::copy_n(x, sz, arr);
}
Or, even better, use std::array
, which works the way you expect.
或者,更好的是,使用std :: array,它以您期望的方式工作。
#2
6
Do you know why this code won't compile?
你知道为什么这段代码不能编译吗?
It won't compile because arrays cannot be copy-initialized (except copy-list-initialized, but you're not doing that).
它不会编译,因为数组不能被复制初始化(除了copy-list-initialized,但你没有这样做)。
Arrays have to be copied one element at a time, using a loop. There's also a standard algorithm for copying so you don't need to write that loop yourself: std::copy
.
必须使用循环一次一个元素复制数组。还有一个标准的复制算法,所以你不需要自己编写那个循环:std :: copy。
Or, you can use std::array
that was introduced in C++11. std::array
is copyable.
或者,您可以使用C ++ 11中引入的std :: array。 std :: array是可复制的。
#3
3
You can't. What you can do is use the std::array wrapper instead:
你不能。您可以做的是使用std :: array包装器:
struct S {
std::array<double, 1> arr;
S(std::array<double, 1> arr) : arr(arr) {}
};
#4
3
Although the signature of a constructor is written like that, the array type is adjusted to a pointer, and the array argument decays to a pointer to its first element.
尽管构造函数的签名是这样写的,但是数组类型被调整为指针,并且数组参数衰减为指向其第一个元素的指针。
However, the arr member in the struct is still of type array. And a pointer can't be assigned to an array (hence the error).
但是,结构中的arr成员仍然是类型数组。并且指针不能分配给数组(因此错误)。
It's also not possible to assign one array to another (e.g. even if you change it to one of the ways described in the above link), so you need to copy the elements manually with a loop, or using std::copy
, or use std::array
, as in Ron's answer.
也不可能将一个数组分配给另一个数组(例如,即使您将其更改为上述链接中描述的方式之一),因此您需要使用循环手动复制元素,或使用std :: copy或使用std :: array,就像Ron的回答一样。
#1
7
Arrays can't be copied.
数组无法复制。
int a[3];
int b[3];
a = b; // illegal
Further, when you pass an array to a function, its name decays to a pointer, so S(double arr[1])
is equivalent to S(double* arr)
. Once you're inside the function, you have to copy the individual elements, so you also need the size of the array:
此外,当您将数组传递给函数时,其名称会衰减为指针,因此S(double arr [1])等效于S(double * arr)。一旦进入函数内部,就必须复制单个元素,因此您还需要数组的大小:
S(double *x, std::size_t sz) {
std::copy_n(x, sz, arr);
}
You can omit the size if you write the template as a function:
如果将模板编写为函数,则可以省略大小:
template <std::size_t sz)
S(double (&x)[sz]) {
std::copy_n(x, sz, arr);
}
Or, even better, use std::array
, which works the way you expect.
或者,更好的是,使用std :: array,它以您期望的方式工作。
#2
6
Do you know why this code won't compile?
你知道为什么这段代码不能编译吗?
It won't compile because arrays cannot be copy-initialized (except copy-list-initialized, but you're not doing that).
它不会编译,因为数组不能被复制初始化(除了copy-list-initialized,但你没有这样做)。
Arrays have to be copied one element at a time, using a loop. There's also a standard algorithm for copying so you don't need to write that loop yourself: std::copy
.
必须使用循环一次一个元素复制数组。还有一个标准的复制算法,所以你不需要自己编写那个循环:std :: copy。
Or, you can use std::array
that was introduced in C++11. std::array
is copyable.
或者,您可以使用C ++ 11中引入的std :: array。 std :: array是可复制的。
#3
3
You can't. What you can do is use the std::array wrapper instead:
你不能。您可以做的是使用std :: array包装器:
struct S {
std::array<double, 1> arr;
S(std::array<double, 1> arr) : arr(arr) {}
};
#4
3
Although the signature of a constructor is written like that, the array type is adjusted to a pointer, and the array argument decays to a pointer to its first element.
尽管构造函数的签名是这样写的,但是数组类型被调整为指针,并且数组参数衰减为指向其第一个元素的指针。
However, the arr member in the struct is still of type array. And a pointer can't be assigned to an array (hence the error).
但是,结构中的arr成员仍然是类型数组。并且指针不能分配给数组(因此错误)。
It's also not possible to assign one array to another (e.g. even if you change it to one of the ways described in the above link), so you need to copy the elements manually with a loop, or using std::copy
, or use std::array
, as in Ron's answer.
也不可能将一个数组分配给另一个数组(例如,即使您将其更改为上述链接中描述的方式之一),因此您需要使用循环手动复制元素,或使用std :: copy或使用std :: array,就像Ron的回答一样。