【问题标题】:C++ expected constant expressionC++ 预期的常量表达式
【发布时间】:2010-03-15 15:41:30
【问题描述】:
#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++;
}

这个程序在我声明 float x[size][2] 的行上给了我预期的常量表达式错误。为什么?

【问题讨论】:

    标签: c++ arrays constant-expression


    【解决方案1】:
    float x[size][2];
    

    这不起作用,因为声明的数组不能具有运行时大小。尝试一个向量:

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

    或者使用新的

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

    如果您没有可用的 C++11,可以使用 boost::array 代替 std::array

    如果您没有可用的 boost,请创建您自己的数组类型,您可以插入向量中

    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]; }
    };
    

    为了简化new 的语法,您可以使用identity 模板,它实际上是就地typedef(也可在boost 中使用)

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

    如果你愿意,也可以使用std::pair&lt;float, float&gt;的向量

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

    【讨论】:

      【解决方案2】:

      数组会在编译时分配,由于size不是常量,编译器无法准确判断其值。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

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

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

          如果您真的想根据仅在运行时拥有的信息分配数组,则需要使用mallocnew 动态分配数组。

          【讨论】:

            【解决方案5】:

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

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

            如果在编译时不知道大小(例如由用户输入,由文件内容确定),则需要使用动态分配,例如:

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

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

            【讨论】:

              【解决方案6】:

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

              C(忽略 C99 的 VLA)和 C++ 中的数组维度必须是编译时已知的数量。这并不意味着只标有const:它们必须被硬编码到程序中。

              使用动态分配或std::vector(它是动态数组分配的包装器)在运行时确定数组大小。

              【讨论】:

                【解决方案7】:

                这是语言的限制。数组大小必须是常量表达式。这是来自 cplusplus.com 的部分解释

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

                【讨论】:

                  【解决方案8】:

                  您还没有为 size 赋值;因此编译器无法为数组分配内存。 (一个空大小的数组?什么?)

                  此外,您需要将 SIZE 设为常量,而不是变量。

                  编辑:不幸的是,由于发帖人已经改变了他们的问题,因此此回复不再有意义。

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2014-06-02
                    • 1970-01-01
                    • 1970-01-01
                    • 2017-07-24
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多