无法将参数 1 从 'Player *' 转换为 'Player' C++
cannot convert parameter 1 from 'Player *' to 'Player' c++
首先,我是c++的新手,我试着四处寻找这个,没有运气。
我正在做一个石头剪子布游戏作为c++作业。在我的主要我想实例化我的对象/类这是我的Main.cpp
#include "Player.h"
#include "Match.h"
#include "ComputerAI.h"
Player* player;
ComputerAI* ai;
Match* match;
int main()
{
player = new Player();
ai = new ComputerAI();
match = new Match(player, ai);
match->StartMatch();
while(true)
{
match->StartNewRound();
}
return 0;
}
我得到的错误是在Match(player, ai)
Error 2 error C2664: 'Match::Match(Player,Player)' : cannot convert parameter 1 from 'Player *' to 'Player'
Match的重载构造函数如下所示:
Match(Player player1, Player player2);
我想我知道错误是什么,因为它基本上说我不能从指针转换为非指针,但我不知道如何解决它。
如果我将构造函数更改为:
Match(Player* player1, Player* player2);
它是快乐的,但这只会给我带来100个新问题。
希望你们能帮帮我。
如果你需要,我可以链接完整的类,但我不确定你是否需要它
看起来,这里并不需要指针。试试这个:
#include "Player.h"
#include "Match.h"
#include "ComputerAI.h"
int main(int argc, char * argv[])
{
// Avoid global variables. Move them
// as local main variables.
Player player;
ComputerAI ai;
Match match;
match.StartMatch();
while(true)
{
match.StartNewRound();
}
return 0;
}
考虑到,如果您接受Player
作为Match
构造函数的参数,您将获得传递实例的副本而不是原件,我猜,这不是您想要的。试一试:
class Match
{
private:
Player & first;
Player & second;
public:
Match(Player & newFirst, Player & newSecond)
: first(newFirst), second(newSecond)
{
// ...
}
};
你可以写
match = new Match( *player, ai );
或者如果构造函数的第二个形参也不是指针,则
match = new Match( *player, *ai );
虽然在你的代码中ai没有类型Player *.
ai = new ComputerAI();
但是我在你的代码中没有看到动态分配玩家的感觉。
我不能评论,因为我还没有足够的声誉?但是如果你使用match构造函数会给你什么错误呢?
Match(Player* player1, Player* player2)
?
我有一个怀疑,但请记住,当它是一个指针调用该类的函数时,您需要使用->操作符。例如,player1->makeMove();
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 复制列表初始化的隐式转换的等级是多少
- 正在将指针转换为范围
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 是否可以从int转换为enum类类型
- 错误:函数"int main()"中请求从 'double' 型转换为非标量类型'Player';
- 无法将参数 1 从 'Player *' 转换为 'Player' C++