【发布时间】:2016-03-10 09:14:55
【问题描述】:
我收到与数组大小有关的错误,错误是:
line 19: expression did not evaluate to a constant <br/>
line 31: array type 'int[*n]' is not assignable <br/>
line 34: array type 'int[*n]' is not assignable <br/>
#include "stdafx.h"
#include "iostream"
int main()
{
using namespace std;
int x=0;
int cst=0;
int cstF=0;
int rst=0;
int n=1;
cout << "insira o numero de consultas" << endl;
cin >> n;
int hora[2 * n];
for (x = 0; x == 2 * n - 2; x += 2)
{
cout << "insira o horario de inicio" << endl;
cin >> cst;
if (hora[x - 2] < cst && hora[x - 1] > cst)
rst = rst;
else
{
rst = rst + 1;
}
hora[x] = cst;
cout << "insira o horario de termino" << endl;
cin >> cstF;
hora[x + 1] = cstF;
}
cout << "o numero de consultas possiveis eh: " << rst << endl;
return 0;
}
我真的很感激我为什么会出错。
【问题讨论】:
-
可变长度数组是一个扩展,而不是 c++ 标准。如果您需要可变长度数组,请改用
std::vector。 -
我认为错误信息很清楚!你的数组大小需要是一个(编译时)常量,并且你给它一些只有在运行时才知道的值。也许您正在寻找
std::vector? -
还有一些文体要点现在要掌握,以免它们太嵌入且难以消除:标准库头文件应包含在
<>中,例如#include <iostream>- 这个告诉系统查找正确的位置,而不是先检查本地目录。另外,请重新考虑您对通常被认为是不良做法的使用:using namespace std;和endl(这些是解释的链接)。 -
如果你想使用
int数组:int *hora = new int [2*n];并在代码末尾:delete hora; -
另外,
hora[x - 2]和hora[x - 1]在x为 0 或 1 时将不起作用。
标签: c++