C++ expected constant expression

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


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

    You cannot have variable length arrays (as they are called in C99) in C++. You need to use dynamically allocated arrays (if the size varies) or a static integral constant expression for size.

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

    The size of an automatic array must be a compile-time constant.

     const int size = 100;
     float x[size][2];
    

    If the size weren't known at compile-time (e.g entered by the user, determined from the contents of the file), you'd need to use dynamic allocation, for example:

    std::vector<std::pair<float, float> > x(somesize);
    

    (Instead of a pair, a dedicated Point struct/class would make perfect sense.)

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