C++ 为什么类函数的参数会出错&引用;“调用”没有匹配的函数;C++;
错误:调用“Player::setX(int&,bool&)”时没有匹配的函数 大体上:C++ 为什么类函数的参数会出错&引用;“调用”没有匹配的函数;C++;,c++,C++,错误:调用“Player::setX(int&,bool&)”时没有匹配的函数 大体上: //player pc.setX(pc.x, *actions); //actions is an arr of input possibilities. pc.setY(pc.y, *actions); 在player.h中: public: int x = 1, y = 1; int setY (int y, int *actions); int setX (i
//player
pc.setX(pc.x, *actions); //actions is an arr of input possibilities.
pc.setY(pc.y, *actions);
在player.h中:
public:
int x = 1, y = 1;
int setY (int y, int *actions);
int setX (int x, int *actions);
第二个问题:是否可以将x/y作为结构而不是单独传递
所有必需的代码:
bool actions[10]; //"up", "down", "left", "right", "skill1", "skill2", "skill3", "skill4", "skill5", "interact",
Player pc; //object creation
//player
pc.setX(pc.x, *actions);
pc.setY(pc.y, *actions);
#ifndef PLAYER_H
#define PLAYER_H
class Player
{
public:
int x = 1, y = 1;
int setY (int y, int *actions);
int setX (int x, int *actions);
private:
};
#endif //
不要将变量类型作为注释。在代码中按原样包含声明。做一个决定。看起来您正在尝试传递一个
bool
而不是int*
。欢迎这样做,向我们展示更多的代码(至少是动作声明),我们可以帮助您。actions
是一个bool[]
数组,其中*actions
访问数组中的第一个bool
。您试图传递一个bool
,其中需要int*
指针,这就是错误告诉您的。你到底想完成什么?至于x/y,当然你可以传递一个struct
,你为什么不这样想呢?bool
数组没有int
地址。如果你试图将数组本身传递给函数,那么将参数从int*
更改为bool*
,然后在调用时将*操作
更改为仅操作
。然后让你自己解释一下指针是如何工作的,这并不能回答问题。OP:s代码中的主要问题是,他/她将bool
而不是int*
传递给actions
参数。
Yes you can pass both variable in a struct as:
typedef struct { int x, int y}sXYInfo;
class Player
{ public:
sXYInfo m_sxy;
int setXY (sXYInfo sxy , int *actions);
};
and when calling use below, as you are passing array as a pointer:
pc.setXY(pc.m_sxy, actions);