C ++ Expression constante attendue
-
20-09-2019 - |
Question
#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++;
}
Ce programme me donne une erreur d'expression constante attendue sur la ligne où je déclare float x [taille] [2]. Pourquoi?
La solution
float x[size][2];
Cela ne fonctionne pas car les tableaux déclarés ne peuvent pas avoir de tailles d'exécution. Essayez un vecteur:
std::vector< std::array<float, 2> > x(size);
Ou utiliser de nouveaux
// identity<float[2]>::type *px = new float[size][2];
float (*px)[2] = new float[size][2];
// ... use and then delete
delete[] px;
Si vous n'avez pas C ++ 11 disponible, vous pouvez utiliser boost::array
à la place de std::array
.
Si vous n'avez pas de boost disponible, créez votre propre type de tableau, vous pouvez rester dans le vecteur
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]; }
};
Pour assouplir la syntaxe de new
, vous pouvez utiliser un identity
modèle qui est effectivement un type de typeef sur place (également disponible dans boost
)
template<typename T>
struct identity {
typedef T type;
};
Si vous le souhaitez, vous pouvez également utiliser un vecteur de std::pair<float, float>
std::vector< std::pair<float, float> > x(size);
// syntax: x[i].first, x[i].second
Autres conseils
The array will be allocated at compile time, and since size
is not a constant, the compiler cannot accurately determine its value.
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.
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
.
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.
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.)
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.
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.