麻烦将整个结构数组传递给函数

时间:2014-08-02 07:06:01

标签: c++ arrays function structure

我对C ++和编程有点新鲜。我正在制作一个口袋妖怪重拍老游戏版本的乐趣,而我在整个结构作为争论时遇到了麻烦。

这是一个缩短版本,以突出我遇到的问题:

struct Enemy_Pokeman
{
    string opp_name;
    int num_pokeman;
    int pokeman_LVL;                  
};   

void pl_Pokeman(Enemy_Pokeman); 

void pokeman_data(string opp_name, int num_pokeman, int pokeman_ID[], int pokeman_LVL[],
                  int purpose)
{
    Enemy_Pokeman enemypokeman[num_pokeman];

    enemypokeman[0].opp_name = opp_name;
    enemypokeman[0].num_pokeman = num_pokeman;
    for(int i=0; i<num_pokeman; i++)
        enemypokeman[i].pokeman_LVL = pokeman_LVL[i];

    pl_Pokeman(enemypokeman);                   //Function call - Codeblocks detects error
                                            //on this line
}

void pl_Pokeman(Enemy_Pokeman enemy)        
{
    cout << endl;
}

对不起,如果这没有意义,我不想发布整件事,所以我把它砍了一下。 问题在于它不会接受Enemy_Pokeman作为争论。

3 个答案:

答案 0 :(得分:1)

传递pl_Pokeman时,

功能Enemy_Pokeman只需an array of Enemy_Pokeman类型

您更新pl_Pokeman function以将数组作为输入:

void pl_Pokeman(Enemy_Pokeman enemy[], int arraySize);

template<typename T, size_t N>
void pl_Pokeman(Enemy_Pokeman (&enemy)[N]) 

答案 1 :(得分:0)

对于单一结构 -

当您将结构作为参数传递时,您应该使用&运算符传递。

pl_Pokeman(&enemypokeman); // Fix 1

捕捉它时,你需要用结构指针捕捉它。

void pl_Pokeman(Enemy_Pokeman *); // Fix 2

For Structure of structure -

pl_Pokeman(&enemypokeman,size); // pass it with size

同时抓住它

void pl_Pokeman(Enemy_Pokeman (*)[], int );

答案 2 :(得分:0)

你传递给你的函数整个Enemy_Pokeman s数组,而不只是一个元素。函数只需要一个元素。另外,您在函数中创建该数组,因此它是一个局部变量。如果函数pokemon_data返回,那么该数组将被销毁。

相关问题