下标的值不是数组,指针或向量,C ++ [英] Subscripted value is not an array, pointer or vector, C++

查看:310
本文介绍了下标的值不是数组,指针或向量,C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我得到了上面的错误(标题中),但是由于某种原因,它只会在第二个循环中抛出此错误。请注意,我使用customer变量的第一个和第二个循环工作正常,没有引发任何错误或其他任何情况。但是在最后一个循环中,在output [customer] [charge]数组上,output [customer]下有一条红线,上面写着下标的值不是数组,指针或向量。我正在使用xcode,Mavericks OSX。我所有的数组都在其他地方定义,并且到现在为止在程序的整个过程中都运行良好。程序中还有其他一些操作在进行,但是它们与该循环无关,所以我只是发布了给出错误的代码。再说一次,charges [customer] [month] [charge]循环工作正常,但是output [customer] [output]无效。

So, I'm getting the above error (in the title) but for some reason it's only throwing this error on the second loop. Notice the first and second loop I have using the customer variable works absolutely fine, no errors thrown or anything. But on that last loop, the output[customer][charge] array, there is a red line under output[customer] that says "Subscripted value is not an array, pointer or vector". I am using xcode, Mavericks OSX. All of my arrays are defined elsewhere, and have worked perfectly the whole length of the program until now. There are some other operations going on in the program, but they have nothing to do with this loop, so I just posted the code that was giving the error. Again I'll say, the charges[customer][month][charge] loop works fine, but the output[customer][output] is not working.

P.S。您可能会认为将所有这些数据保留在数字索引数组中的逻辑很愚蠢,但这是针对学校项目的。因此,请不要告诉我该程序在逻辑上是如何前后一致的或其他内容。谢谢!

P.S. You probably will think the logic behind keeping all this data in numerically indexed arrays is dumb, but it is for a school project. So don't lecture me about how this program is logically inconsistent or whatever. Thanks!

string headings[3][7];
string chargeLabels[3] = {"Electricity :","Water: ","Gas: "};
string outputLabels[5] = {"Subtotal: ","Discount: ","Subtotal: ","Tax: ","Total: "};
double charges[3][3][3];
double output[3][5];

for(int customer=0; customer<3; customer++)
{
    for(int heading=0; heading<5; heading++)
    {
        cout << headings[customer][heading];
    }

    for(int month=0; month<3; month++)
    {
        cout << chargeLabels[month];

        for(int charge=0; charge<3; charge++)
        {
            cout << charges[customer][month][charge] << ", ";
        }
        cout << endl;
    }
    for(int output=0; output<5; output++)
    {
        cout << outputLabels[output];
        //error is below this comment
        cout << output[customer][output] << endl;
    }
}


推荐答案

内部 for 语句:

for(int output=0; output<5; output++)
{

您声明了另一个变量 int输出会遮盖双重输出[3] [5] ,在之外为声明。

You declared another variable int output which shadows the double output[3][5] with the same name outside the for statement.

这篇关于下标的值不是数组,指针或向量,C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆