C++ expected constant expression

前端 未结 8 1471
日久生厌
日久生厌 2020-11-30 10:42
#include 
#include 
#include 
#include 
#include 
using std::ifstream;
using namespace std;
         


        
相关标签:
8条回答
  • 2020-11-30 11:07

    The line float x[size][2] won't work, because arrays have to be allocated at compile time (with a few compiler-specific exceptions). If you want to be able to easily change the size of the array x at compile time, you can do this:

     #define SIZE 100
     float x[SIZE][2];
    

    If you really want to allocate the array based on information you only have at runtime, you need to allocate the array dynamically with malloc or new.

    0 讨论(0)
  • 2020-11-30 11:08

    You haven't assigned any value to size; thus the compiler cannot allocate the memory for the array. (An array of null size? What?)

    Additionally, you'd need to make SIZE a constant, and not a variable.

    EDIT: Unfortunately, this response no longer makes sense since the poster has changed their question.

    0 讨论(0)
  • 2020-11-30 11:11

    Because it expected a constant expression!

    Array dimensions in C (ignoring C99's VLAs) and C++ must be quantities known at compile-time. That doesn't mean just labelled with const: they have to be hard-coded into the program.

    Use dynamic allocation or std::vector (which is a wrapper around dynamic array allocation) to determine array sizes at run-time.

    0 讨论(0)
  • 2020-11-30 11:16

    It is a restriction of the language. Array sizes must be constant expressions. Here's a partial jsutification from cplusplus.com

    NOTE: The elements field within brackets [] which represents the number of elements the array is going to hold, must be a constant value, since arrays are blocks of non-dynamic memory whose size must be determined before execution. In order to create arrays with a variable length dynamic memory is needed, which is explained later in these tutorials.

    0 讨论(0)
  • 2020-11-30 11:21
    float x[size][2];
    

    That doesn't work because declared arrays can't have runtime sizes. Try a vector:

    std::vector< std::array<float, 2> > x(size);
    

    Or use new

    // identity<float[2]>::type *px = new float[size][2];
    float (*px)[2] = new float[size][2];
    
    // ... use and then delete
    delete[] px;
    

    If you don't have C++11 available, you can use boost::array instead of std::array.

    If you don't have boost available, make your own array type you can stick into vector

    template<typename T, size_t N>
    struct array {
      T data[N];
      T &operator[](ptrdiff_t i) { return data[i]; }
      T const &operator[](ptrdiff_t i) const { return data[i]; }
    };
    

    For easing the syntax of new, you can use an identity template which effectively is an in-place typedef (also available in boost)

    template<typename T> 
    struct identity {
      typedef T type;
    };
    

    If you want, you can also use a vector of std::pair<float, float>

    std::vector< std::pair<float, float> > x(size);
    // syntax: x[i].first, x[i].second
    
    0 讨论(0)
  • 2020-11-30 11:26

    The array will be allocated at compile time, and since size is not a constant, the compiler cannot accurately determine its value.

    0 讨论(0)
提交回复
热议问题