【发布时间】:2014-08-23 06:09:46
【问题描述】:
// testing1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <iostream>
#include<conio.h>
using namespace std;
struct Data_point {
double x;
double y;
};
void PlotThis(unsigned int n )
{
Data_point graph[n]; //shows error here
//do something else, dont worry about that
}
int main ()
{
unsigned int nSamples;
cout << "Please enter nSamples: ";
cin >> nSamples;
PlotThis(nSamples);
return 0;
}
编译时显示错误:
Error 1 error C2057: expected constant expression testing1.cpp 23
Error 2 error C2466: cannot allocate an array of constant size 0 testing1.cpp 23
Error 3 error C2133: 'graph' : unknown size testing1.cpp 23
第23行是Data_point graph[n]; //这里显示错误
即使我将 main() 中的值传递给它,它仍显示未知大小。它在编译时要求值(图的大小,即 n)。这是否意味着数组大小分配发生在编译时?如何解决这个问题
【问题讨论】:
-
在 ubuntu 12 上使用 g++ 4.6.3 编译时没有错误
-
@Vink,使用
-pedantic编译(或-pedantic-errors以实际匹配您的声明)。 -
相关问题提供了更多关于此处被视为常量的详细信息:Does “int size = 10;” yield a constant expression?。
标签: c++