我收到了这个错误:
error C2664: 'KaibaMainPhase' : cannot convert parameter 1 from
'void (__cdecl *[2])(std::vector<_Ty> &,
std::vector<_Ty>&,
std::vector<std::vector<_Ty>> &,
std::vector<_Ty> &,std::vector<_Ty> &)'
to 'void (__cdecl *)(std::vector<_Ty> &,
std::vector<Card> &,
std::vector<Card> &)"
这是我的代码:
class Card
{
public:
string GetName() {return Name;}
void SetName(const string& setName) {Name = setName; }
void SetType(const string& setType) {Type = setType; }
private:
string Name;
string Type;
};
vector< vector<Card> > Field(4, vector<Card>(5));
vector<Card> KaibaHand;
vector<Card>::iterator KH;
vector<Card> YugiHand;
vector<Card>::iterator YH;
vector<Card> KaibaGraveyard;
vector<Card>::iterator KG;
vector<Card> YugiGraveyard;
vector<Card>::iterator YG;
void KaibaBeginning(vector<Card> &KaibaHand, vector<Card> &KaibaDeck);
void DrawForKaiba(vector<Card> &KaibaHand, vector<Card> &KaibaDeck);
void KaibaMainPhase(void (*)(vector< vector<Card> > &Field, vector<Card> &KaibaGraveyard,
vector<Card> &YugiGraveyard), vector<Card> &KaibaHand, vector<Card> &KaibaDeck, vector<
vector<Card> > &Field, vector<Card> &YugiGraveyard, vector<Card> &KaibaGraveyard);
void FieldFunction(vector< vector<Card> > &Field);
void KaibaHandFunction(vector<Card> &KaibaHand);
void KaibaGraveyardFunction(vector<Card> &KaibaGraveyard);
void YugiGraveyardFunction(vector<Card> &YugiGraveyard);
void KaibaSummon(vector< vector<Card> > &Field, vector<Card> &KaibaHand, int HandNumber, int
MonstCount);
void YugiSummon(vector< vector<Card> > &Field, vector<Card> &YugiHand, int HandNumber, int
MonstCount);
void KaibaDiscard(int HandNumber, vector<Card> &KaibaHand, vector<Card> &KaibaGraveyard);
void YugiDiscard(int HandNumber, vector<Card> &YugiHand, vector<Card> &YugiGraveyard);
void KaibaMainPhase(void (*)(vector< vector<Card> > &Field, vector<Card> &KaibaGraveyard,
vector<Card> &YugiGraveyard), vector<Card> &KaibaHand, vector<Card> &KaibaDeck,
vector<vector<Card> > &Field, vector<Card> &YugiGraveyard, vector<Card> &KaibaGraveyard);
void HEAVYSTORM(vector<Card> &KaibaHand, vector<Card> &KaibaDeck, vector< vector<Card> > &Field,
vector<Card> &KaibaGraveyard, vector<Card> &YugiGraveyard)
{
cout<<"This card destroys ALL magic and traps cards on the field!!"<<endl;
for (int i=0; i<5; i++)
{
YugiGraveyard.push_back(Field[0][i]);
Field[0][i]=Card();
KaibaGraveyard.push_back(Field[3][i]);
Field[3][i]=Card();
}
}
void POTOFGREED(vector<Card> &KaibaHand, vector<Card> &KaibaDeck, vector< vector<Card> > &Field,
vector<Card> &KaibaGraveyard, vector<Card> &YugiGraveyard)
{
DrawForKaiba(KaibaHand, KaibaDeck);
}
int main()
{
void (* pFunct[2]) (vector<Card> &KaibaHand, vector<Card> &KaibaDeck, vector< vector<Card> >
&Field, vector<Card> &KaibaGraveyard, vector<Card> &YugiGraveyard);
pFunct[0] = HEAVYSTORM;
pFunct[1] = POTOFGREED;
KaibaMainPhase(pFunct, KaibaHand, KaibaDeck, Field, YugiGraveyard, KaibaGraveyard);
}
void KaibaMainPhase( void (*pFunct)(vector<Card> &KaibaHand, vector<Card> &KaibaDeck, vector<
vector<Card> > &Field, vector<Card> &KaibaGraveyard, vector<Card> &YugiGraveyard), vector<Card>
&KaibaHand, vector<Card> &KaibaDeck, vector< vector<Card> > &Field, vector<Card> &YugiGraveyard,
vector<Card> &KaibaGraveyard)
{
int HandNum;
cout<<"Enter a number: "<<endl;
cin>>HandNum;
cout<<"You played "<<Field[3][HandNum-1].GetName();
pFunct[HandNum-1](KaibaHand, KaibaDeck, Field, KaibaGraveyard, YugiGraveyard);//
}
void KaibaHandFunction(vector<Card> &KaibaHand)
{
cout<<endl<<"Kaiba's hand contains: "<<endl;
for (KH=KaibaHand.begin(); KH<KaibaHand.end(); KH++)
{
cout<<KH->GetName()<<endl;
}
}
void YugiHandFunction(vector<Card> &YugiHand)
{
cout<<endl<<"Yugi's hand contains: "<<endl;
for (YH=YugiHand.begin(); YH<YugiHand.end(); YH++)
{
cout<<YH->GetName()<<endl;
}
}
void KaibaGraveyardFunction(vector<Card> &KaibaGraveyard)
{
cout<<endl<<"Kaiba's graveyard contains: "<<endl;
for (KG=KaibaGraveyard.begin(); KG<KaibaGraveyard.end(); KG++)
{
cout<<KG->GetName()<<endl; //SAME AS (*gt).GetName
}
cout<<"\n";
}
void YugiGraveyardFunction(vector<Card> &YugiGraveyard)
{
cout<<endl<<"Yugi's graveyard contains: "<<endl;
for (YG=YugiGraveyard.begin(); YG<YugiGraveyard.end(); YG++)
{
cout<<YG->GetName()<<endl; //SAME AS (*gt).GetName
}
cout<<"\n";
答案 0 :(得分:8)
在这一行:
void KaibaMainPhase(void (*)(vector< vector<Card> > &Field, vector<Card> &KaibaGraveyard, vector<Card> &YugiGraveyard), vect....
您声明KaibaMainPhase
的第一个参数是3个参数的函数。
void KaibaMainPhase(
/* Function Pointer */ void (*)(
/* Param #1 */ vector< vector<Card> > &Field,
/* Param #2 */ vector<Card> &KaibaGraveyard,
/* Param #3 */ vector<Card> &YugiGraveyard), /*other parameters to KaibaMainPhase*/....
稍后,在此代码中:
pFunct[0] = HEAVYSTORM;
[...]
KaibaMainPhase(pFunct, KaibaHand, Kaiba.....
您正在传递pFunct
,这是两个不同功能的数组
数组与函数指针不同。
但是,即使你确实通过了pFunct[0]
,它也行不通。
pFunct
被声明为函数指针数组,其中每个函数都有 5 参数:
/* Array of 2 fnct ptrs */ void (* pFunct[2])(
/* Param #1 */ vector<Card> &KaibaHand,
/* Param #2 */ vector<Card> &KaibaDeck,
/* Param #3 */ vector< vector<Card> > &Field,
/* Param #4 */ vector<Card> &KaibaGraveyard,
/* Param #5 */ vector<Card> &YugiGraveyard
);
但是函数KaibaMainPhase
期望一个只有 3 参数的函数。