C++编程问题 error:C2466

为什么输入以下代码在C++会报错?n的值不应该是3吗?

string a = "abc";
const int n = a.size();
bool f[n][n];

错误:error C2466: cannot allocate an array of constant size 0

这个主要是编译器的问题,用g++就没有问题
在vc中定义数组,需要一个常量值
下面是g++编译后的结果
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;

int main()
{
string str = "abc";
const int n = str.size();
bool f[n][n];
f[0][1] = true;
cout<<f[0][1]<<endl;
system("pause");
return 0;
}

 

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-01-03
  数组的维数必须用值大于等于 1 的常量表达式定义。此常量表达式只能包含整型字面值常量、枚举常量或者用常量表达式初始化的整型 const 对象。非 const 变量以及要到运行阶段才知道其值的 const变量都不能用于定义数组的维数。
第2个回答  2015-01-03
不同版本的IDE,不同的规则!在VC里面没错,去到其他又不行了,你这在.NET没错,在VC6又错了!