C ++ expected constant expression

#include <iostream> #include <fstream> #include <cmath> #include <math.h> #include <iomanip> using std::ifstream; using namespace std; int main (void) { int count=0; float sum=0; float maximum=-1000000; float sumOfX; float sumOfY; int size; int negativeY=0; int positiveX=0; int negativeX=0; ifstream points; //the points to be imported from file //points.open( "data.dat"); //points>>size; //cout<<size<<endl; size=100; float x[size][2]; while (count<size) { points>>(x[count][0]); //cout<<"x= "<<(x[count][0])<<" ";//read in x value points>>(x[count][1]); //cout<<"y= "<<(x[count][1])<<endl;//read in y value count++; } 

This program gives me the expected constant expression error in the line where I declare float x [size] [2]. Why?

+7
c ++ arrays constant-expression
source share
8 answers
 float x[size][2]; 

This does not work because declared arrays cannot have run-time sizes. Try the vector:

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

Or use a new one

 // 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, you can use boost::array instead of std::array .

If you don't have a raise, make your own array type, which you can insert into the 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]; } }; 

To loosen new syntax, you can use the identity template, which is actually a built-in typedef (also available in boost )

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

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

 std::vector< std::pair<float, float> > x(size); // syntax: x[i].first, x[i].second 
+11
source share

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

+8
source share

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

+4
source share

The string float x[size][2] will not work because arrays must be allocated at compile time (with a few exceptions for the compiler). If you want to easily resize the x array at compile time, you can do this:

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

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

+2
source share

This is a language restriction. The dimensions of the array must be constant. Here's a partial jsutification from cplusplus.com

NOTE. The field of elements in brackets [], which represents the number of elements that the array will hold, should be a constant value, since arrays are blocks of non-dynamic memory, the size of which must be determined before execution. Creating variable-length arrays requires dynamic memory, which is explained later in these tutorials.

+1
source share

You have not assigned any value to the size; therefore, the compiler cannot allocate memory for the array. (Array of zero size? What?)

In addition, you need to make SIZE a constant, not a variable.

EDIT: Unfortunately, this answer no longer makes sense as the poster has changed its question.

+1
source share

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

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

If the size was not known at compile time (for example, entered by the user, determined from the contents of the file), you will need to use dynamic allocation, for example:

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

(Instead of a pair, the highlighted struct / class point makes sense.)

+1
source share

Because he expected constant expression!

Array sizes in C (excluding C99 VLAs) and C ++ must be known at compile time. This does not mean that you are simply labeled const : they must be hardcoded in the program.

Use dynamic allocation or std::vector (which is a wrapper around the placement of a dynamic array) to determine the size of the array at runtime.

+1
source share

All Articles