我正在使用动态内存分配来创建新对象,当我尝试编译时,以下错误不断出现。我将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();
}
答案 0 :(得分:1)
dimensions_
是从unsigned int
返回的指针,而不是new unsigned int (temp);
您需要magnitude_ = new double [*dimensions_];
答案 1 :(得分:1)
您不能将指针用作数组大小。
&#34;在运行时计算的大小数组的一般解决方案&#34;是使用std::vector
。