[英]Trouble passing a whole structure array to a function
I'm sort of new to C++ and programming in general. 一般而言,我对C ++和编程还是有点陌生。 I'm making a pokemon remake of the old gameboy version for fun, and I'm having trouble passing a whole structure as an arguement.
我正在为旧的Gameboy版本制作宠物小精灵重制版,很有趣,但我很难通过整个结构作为争论。
This is a shortened version to highlight the problem I'm having: 这是简化版,以突出我遇到的问题:
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;
}
Sorry if this doesn't make sense, I didn't want to post the entire thing, so I chopped it up a bit. 抱歉,如果这没有意义,我不想发布整个内容,因此我将其切碎了。 The problem is that it won't accept
Enemy_Pokeman
as an arguement. 问题在于它不会接受
Enemy_Pokeman
的争论。
Function pl_Pokeman
only takes Enemy_Pokeman
type while you passed in an array of Enemy_Pokeman
当您传入
an array of Enemy_Pokeman
函数pl_Pokeman
仅采用Enemy_Pokeman
类型
You update pl_Pokeman function
to take array as input: 您更新
pl_Pokeman function
以将数组作为输入:
void pl_Pokeman(Enemy_Pokeman enemy[], int arraySize);
Or 要么
template<typename T, size_t N>
void pl_Pokeman(Enemy_Pokeman (&enemy)[N])
For Single structure- 对于单一结构-
When you are passing the structure as a argument, you should pass with &
operator. 当您将结构作为参数传递时,应与
&
运算符一起传递。
pl_Pokeman(&enemypokeman); // Fix 1
While catching it you need to catch it with Structure pointer. 捕获它时,您需要使用Structure指针捕获它。
void pl_Pokeman(Enemy_Pokeman *); // Fix 2
For Array of structure- 对于结构数组-
pl_Pokeman(&enemypokeman,size); // pass it with size
while catching it 在抓住它的同时
void pl_Pokeman(Enemy_Pokeman (*)[], int );
you're passing to your function whole array of Enemy_Pokeman
s, not just one element. 您将整个
Enemy_Pokeman
数组传递给函数,而不仅仅是一个元素。 function expects one element only. 函数只需要一个元素。 also, you're creating that array within a function, so it's a local variable.
同样,您正在函数中创建该数组,因此它是一个局部变量。 if function
pokemon_data
returns, that array will be destroyed. 如果函数
pokemon_data
返回,则该数组将被销毁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.