Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;期望常数表达式 #包括 #包括 #包括 #包括 #包括 使用std::ifstream; 使用名称空间std; 内部主(空) { 整数计数=0; 浮点数和=0; 浮动最大值=-1000000; 浮点数x; 浮球; 整数大小; int negativeY=0; int-positiveX=0; int negativeX=0; ifstream points;//要从文件导入的点 //点。打开(“data.dat”); //点数>>大小; //couth_C++_Arrays_Constant Expression - Fatal编程技术网

C++ C++;期望常数表达式 #包括 #包括 #包括 #包括 #包括 使用std::ifstream; 使用名称空间std; 内部主(空) { 整数计数=0; 浮点数和=0; 浮动最大值=-1000000; 浮点数x; 浮球; 整数大小; int negativeY=0; int-positiveX=0; int negativeX=0; ifstream points;//要从文件导入的点 //点。打开(“data.dat”); //点数>>大小; //couth

C++ C++;期望常数表达式 #包括 #包括 #包括 #包括 #包括 使用std::ifstream; 使用名称空间std; 内部主(空) { 整数计数=0; 浮点数和=0; 浮动最大值=-1000000; 浮点数x; 浮球; 整数大小; int negativeY=0; int-positiveX=0; int negativeX=0; ifstream points;//要从文件导入的点 //点。打开(“data.dat”); //点数>>大小; //couth,c++,arrays,constant-expression,C++,Arrays,Constant Expression,数组将在编译时分配,由于大小不是常数,编译器不能准确地确定其值。 在C++中不能有可变长度数组(如C99中所调用的数组)。需要使用动态分配的数组(如果大小不同)或大小的静态整数常量表达式。这是该语言的限制。数组大小必须是常量表达式。以下是cplusplus.com的部分jsutification 注:括号内的元素字段[]它表示数组将容纳的元素数,必须是常量,因为数组是非动态内存块,其大小必须在执行之前确定。为了创建具有可变长度的数组,需要动态内存,这将在这些教程后面进行解释 行float x[s

数组将在编译时分配,由于<代码>大小<代码>不是常数,编译器不能准确地确定其值。

在C++中不能有可变长度数组(如C99中所调用的数组)。需要使用动态分配的数组(如果大小不同)或大小的静态整数常量表达式。

这是该语言的限制。数组大小必须是常量表达式。以下是cplusplus.com的部分jsutification

注:括号内的元素字段[]它表示数组将容纳的元素数,必须是常量,因为数组是非动态内存块,其大小必须在执行之前确定。为了创建具有可变长度的数组,需要动态内存,这将在这些教程后面进行解释


float x[size][2]
不起作用,因为数组必须在编译时分配(有一些特定于编译器的异常)。如果希望能够在编译时轻松更改数组
x
的大小,可以执行以下操作:

#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++;
}

如果您确实想根据运行时仅有的信息分配数组,则需要使用
malloc
new
动态分配数组,因为您没有为大小分配任何值;因此编译器无法为数组分配内存。(空大小的数组?什么?)

此外,您需要将大小设置为常量,而不是变量

编辑:不幸的是,由于海报改变了他们的问题,这个回答不再有意义

 #define SIZE 100
 float x[SIZE][2];
这不起作用,因为声明的数组不能具有运行时大小。请尝试向量:

float x[size][2];
为了简化
new
的语法,可以使用
identity
模板,该模板实际上是一个就地typedef(也可在
boost
中获得)

模板
结构标识{
T型;
};

如果需要,还可以使用
std::pair

std::vectorx(大小);
//语法:x[i]。第一,x[i]。第二

自动数组的大小必须是编译时常量

std::vector< std::pair<float, float> > x(size);
// syntax: x[i].first, x[i].second
如果编译时不知道大小(例如,由用户输入,根据文件内容确定),则需要使用动态分配,例如:

 const int size = 100;
 float x[size][2];
std::向量x(某种大小);

(而不是一对,一个专用的点结构/类将非常有意义。)

因为它需要一个常量表达式

<>数组中的维数(忽略C99的VLAS)和C++必须是编译时已知的量,这并不意味着只是用<代码> const >:它们必须硬编码到程序中。

使用动态分配或
std::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]; }
};
template<typename T> 
struct identity {
  typedef T type;
};
std::vector< std::pair<float, float> > x(size);
// syntax: x[i].first, x[i].second
 const int size = 100;
 float x[size][2];
std::vector<std::pair<float, float> > x(somesize);