2015-09-09 52 views
-3

我遇到了一个非常奇怪的C++错误,我无法完成。我以前初始化数百个阵列,但由于某些原因,当我让双打的数组:数组下标的类型无效

double trap[]={0.0,0.0,0.0,0.0,0.0,0.0}; 

然后尝试做任何事情与此阵的具体内容,即:

cout << trap[3] << endl; 

我得到了奇怪的错误:

compare.cpp:43:20: error: invalid types ‘double[int]’ for array subscript 
cout << trap[3] << endl; 

我根本不理解。为什么下标(整数)与该元素中包含的值的类型(双精度)有什么关系?

编辑:这是我认为是给错误全码:

#include <stdio.h> 
#include <math.h> 
#include <cmath> 
#include <iostream> 
#include <iomanip> 

using namespace std; 

const double Xmax[]={2.0,10.0,20.0,40.0, 80.0, 160.0}; 

double f (double x) { 
    return (exp(-x));    
} 

int main() { 
    FILE *Trap = fopen("trap.dat","w"); 

    double trap[]={0.0,0.0,0.0,0.0,0.0,0.0}, 

cout << gaus2_old[3] << endl; 
//...do other stuff with trap[] and Xmax[]... 
} 

这里的一切在该行“做其他的东西与陷阱[]和的Xmax []”是我的代码只是休息,其中阱的每个实例[]和的Xmax []给出相同的错误:

compare.cpp:43:20: error: invalid types ‘double[int]’ for array 
+0

_“(为什么不是这个代码的工作?“)寻求帮助调试问题”必须包括所期望的行为,一个特定的问题或错误,并重现它在问题所必需的最短代码本身。没有明确问题陈述的问题对其他读者无益。请参阅:[**如何创建一个最小,完整和可验证的示例**](http://stackoverflow.com/help/mcve)。 “_ –

+2

您的示例[正常工作](https://ideone.com/JJq17I)。 – alain

+0

这两行代码可以正常编译。错误必须在其他地方。您能否向我们展示大部分代码? –

回答

1

我运行在VS此代码和它打印0到控制台。

// testcpp.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include <iostream> 

using namespace std; 
int _tmain(int argc, _TCHAR* argv[]) 
{ 
    double trap[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; 
    cout << trap[3] << endl; 

    return 0; 
} 

上更新:我同意https://stackoverflow.com/users/5289220/sukces-elektroniki,更改

double trap[]={0.0,0.0,0.0,0.0,0.0,0.0}, 

double trap[]={0.0,0.0,0.0,0.0,0.0,0.0}; 

变化

cout << gaus2_old[3] << endl; 

cout << trap[3] << endl; 
+0

一旦问题得到澄清,这个答案可能会“陈旧” –

+0

对不起!我在编辑中添加了上面的“完整”代码。我试着在多台机器上编译它,每次尝试调用数组中的任何特定元素时,我总是会得到相同的错误。 – khfrekek

相关问题