Search code examples
c++arraysdynamic-memory-allocationintegralfunction-templates

error: size in array new must have integral type [-fpermissive]


I'm using dynamic memory allocation to create new objects and the following error keeps showing up when I try to compile. I have dimensions_ created as an unsigned int so I'm not sure why this error shows up.

EuclideanVector.h:69:40: error: size in array new must have integral type [-fpermissive]
                         magnitude_  =  new double [dimensions_];

The following is the code where the error points to:

// 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();
      }

Solution

  • dimensions_ is a pointer, not an unsigned int, returned from new unsigned int (temp);.
    You need something like magnitude_ = new double [*dimensions_];