error: size in array new must have integral type [-fpermissive]
error: size in array new must have integral type [-fpermissive]
我正在使用动态内存分配来创建新对象,但在我尝试编译时不断出现以下错误。我将 dimensions_ 创建为 unsigned int,所以我不确定为什么会出现此错误。
EuclideanVector.h:69:40: error: size in array new must have integral type [-fpermissive]
magnitude_ = new double [dimensions_];
错误指向的代码如下:
// target constructor for delegating constructor
template <typename NUM1, typename NUM2> // any numeric types of user input for dimensions and magnitude will be static_cast to unsigned int and double respectively
EuclideanVector(const NUM1 dimensions, const NUM2 magnitude){
// static cast to unsigned int for temp and assign dimensions_ to that
unsigned int temp = static_cast<unsigned int>(dimensions);
dimensions_ = new unsigned int (temp);
// assign pointer "magnitude_" to dynamically-allocated memory of new unnamed array<double> object of size "dimensions_"
magnitude_ = new double [dimensions_];
// fill the array<double> object "magnitude_" a number of "dimensions_" times, with the <double> value of "magnitude_" for each dimension
std::fill_n(magnitude_, dimensions_, static_cast<double>(magnitude));
updateNormal();
}
dimensions_
是一个指针,而不是 unsigned int
,从 new unsigned int (temp);
返回。
你需要像 magnitude_ = new double [*dimensions_];
这样的东西
不能使用指针作为数组大小。
"array of size computed at run-time" 的一般解决方案是使用 std::vector
。
我正在使用动态内存分配来创建新对象,但在我尝试编译时不断出现以下错误。我将 dimensions_ 创建为 unsigned int,所以我不确定为什么会出现此错误。
EuclideanVector.h:69:40: error: size in array new must have integral type [-fpermissive]
magnitude_ = new double [dimensions_];
错误指向的代码如下:
// target constructor for delegating constructor
template <typename NUM1, typename NUM2> // any numeric types of user input for dimensions and magnitude will be static_cast to unsigned int and double respectively
EuclideanVector(const NUM1 dimensions, const NUM2 magnitude){
// static cast to unsigned int for temp and assign dimensions_ to that
unsigned int temp = static_cast<unsigned int>(dimensions);
dimensions_ = new unsigned int (temp);
// assign pointer "magnitude_" to dynamically-allocated memory of new unnamed array<double> object of size "dimensions_"
magnitude_ = new double [dimensions_];
// fill the array<double> object "magnitude_" a number of "dimensions_" times, with the <double> value of "magnitude_" for each dimension
std::fill_n(magnitude_, dimensions_, static_cast<double>(magnitude));
updateNormal();
}
dimensions_
是一个指针,而不是 unsigned int
,从 new unsigned int (temp);
返回。
你需要像 magnitude_ = new double [*dimensions_];
不能使用指针作为数组大小。
"array of size computed at run-time" 的一般解决方案是使用 std::vector
。