[英]Trouble passing a whole structure array to a function
一般而言,我对C ++和编程还是有点陌生。 我正在为旧的Gameboy版本制作宠物小精灵重制版,很有趣,但我很难通过整个结构作为争论。
这是简化版,以突出我遇到的问题:
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
的争论。
当您传入an array of Enemy_Pokeman
函数pl_Pokeman
仅采用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])
对于单一结构-
当您将结构作为参数传递时,应与&
运算符一起传递。
pl_Pokeman(&enemypokeman); // Fix 1
捕获它时,您需要使用Structure指针捕获它。
void pl_Pokeman(Enemy_Pokeman *); // Fix 2
对于结构数组-
pl_Pokeman(&enemypokeman,size); // pass it with size
在抓住它的同时
void pl_Pokeman(Enemy_Pokeman (*)[], int );
您将整个Enemy_Pokeman
数组传递给函数,而不仅仅是一个元素。 函数只需要一个元素。 同样,您正在函数中创建该数组,因此它是一个局部变量。 如果函数pokemon_data
返回,则该数组将被销毁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.