简体   繁体   English

将2D数组索引传递给函数?

[英]Pass a 2D Array Index into a Function?

I have a loop that populates a 2D array, and I want to pass the values created by this population, into a function to do other calculations with it. 我有一个填充2D数组的循环,我想将由此总体创建的值传递到函数中,以使用它进行其他计算。 I'm a beginner at C++, so clear explanations would help a lot. 我是C ++的初学者,因此清楚的说明会很有帮助。 Here is my code: 这是我的代码:

for (int car = 1; car <= 27; car++) {

    int test[27][3] = {{car, mpg[car], speed[car]}};

    float speed = speed[car];

    timeGen(speed);

    cout << car << "\t" << mpg[car] << "\t" << speed[car] << endl;
}

This is the timeGen function: 这是timeGen函数:

float timeGen(float x)
{
int distance = 50;
float speed = x;
float time = distance/x;
return time;
}

It seems as though everything will work fine, but what happens is that I get an error saying "subscript requires array or pointer type." 似乎一切正常,但是发生的是我收到一条错误消息,说“下标需要数组或指针类型”。 I'm a little confused as to what they mean. 我对它们的含义有些困惑。 Is it telling me to create a pointer to this index, and then call the pointer in the timeGen function? 它是否告诉我创建一个指向该索引的指针,然后在timeGen函数中调用该指针? An explanation would be great! 一个解释会很好!

Thank you very much. 非常感谢你。 Also, the values mpg , speed , are taken from another part of my code which works fine. 另外,值mpgspeed也是从我的代码的另一部分取得的,该部分工作正常。 Instructions on how to fix the issue I'm having right now, would be great! 关于如何解决我现在遇到的问题的说明非常棒!

Even if you have an array named speed before defining float speed , it goes out of scope right after this definition. 即使在定义float speed speed之前有一个名为speed的数组,该数组也会在定义之后超出范围。 Try this: 尝试这个:

float Cur_speed = speed[car];
timeGen(Cur_speed);

or 要么

timeGen(speed[car]); // without the float speed

Another thing is, at each iteration you are creating a new instance of test . 另一件事是,在每次迭代中,您都在创建一个新的test实例。 It looks like what you really want is to have a single test array: 看起来您真正想要的是拥有一个test数组:

int test[27][3];
for (int car = 0; car < 27; car++) {
 test[car][0] = car;
 test[car][1] = mpg[car];
 test[car][2] = speed[car];

Make sure that mpg is an array with 27 elements, and always use indices from 0 to 26. Same for the speed array. 确保mpg是一个包含27个元素的数组,并且始终使用从0到26的索引。对于speed数组,该参数相同。 When we say test[car][0] , we mean the first element of test[car] , which has a total of 3 elements due to your definition. 当我们说test[car][0] ,是指test[car]的第一个元素,根据您的定义,总共有3个元素。 Essentially, the lines test[car][X] = Y; 本质上,行test[car][X] = Y; do the same thing you want to do with test[27][3] = {{car, mpg[car], speed[car]}} , however that syntax only works when you first declare an array. 使用test[27][3] = {{car, mpg[car], speed[car]}} ,但是该语法仅在您首次声明数组时有效。 And the way you write it initializes all 27 elements with the same data. 编写方式将使用相同的数据初始化所有27个元素。

You need to define float car_speed rather than float speed . 您需要定义float car_speed而不是float speed It will resolve your error. 它将解决您的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM