简体   繁体   English

在通过函数传递整个数组时遇到麻烦

[英]Having some trouble passing an entire array through a function

Fairly simple problem I'm having that I can't get my head around. 我遇到的一个非常简单的问题是我无法解决问题。 I'm trying to pass an entire array into a function, and it's giving me a syntax error. 我正在尝试将整个数组传递给函数,这给了我一个语法错误。 But I honestly can't see the error, even though it's clearly right in front of me. 但是,老实说我看不到错误,即使它很明显就在我眼前。

Function Declaration: 功能声明:

void insertWholeStruct(StoredData temp[]);

Function Definition: 功能定义:

void Array::insertWholeStruct(StoredData temp[])
{
    for(int i=0;i<arrSize;i++)
    {

        arr[i] = temp[i].id;
        }
    }

Function call, where the error is: 函数调用,错误为:

test.insertWholeStruct(testData[]);

只需在函数调用中的testData之后删除[]

The error is in the way you try to pass the argument in the function. 错误是您尝试在函数中传递参数的方式。 Your compiler is complaining for not knowing at compile time the size of the array you are trying to pass. 您的编译器抱怨在编译时不知道您要传递的数组的大小。 Pass by pointer the array and this is alliviated. 通过指针传递数组,这将被消除。

It should be instead : 应该改为:

//no need to define pointer name in declaration
void insertWholeStruct(StoredData *);


//function definition should change only a little bit
void insertWholeStruct(StoredData *temp)
{

   for(int i=0;i<arrSize;i++)
   {
       arr[i] = temp[i].id;
   }
}

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

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