c++ - C ++:将对象作为参数传递
问题描述
我只是想知道为什么这段代码不能编译:
#include <iostream>
class Player
{
public:
Player()
{
}
};
class Game
{
public:
Game()
{
}
void getPlayer(Player &player)
{
}
};
int main()
{
Game *game = new Game();
Player *player = new Player();
game->getPlayer(&player);
return 0;
}
我想将玩家对象作为参数传递给 Game 类的 getPlayer() 方法。
问候
解决方案
你的方法:
void getPlayer(Player &player)
接受对类型对象的左值引用,Player
但您试图将其传递Player **
给它,以修复编译错误,只需取消引用您的指针:
game->getPlayer(*player);
但是您的程序设计看起来不正确(将引用传递给调用的方法getPlayer
等)
推荐阅读
- python - 如何通过使用它们之间的现有交集来更改数据集以匹配另一个类似的 -warped- ?
- sql-server - SQL Server 2016二次经常发出错误3456,如何防止SQL数据库错误3456?
- javascript - 顶部菜单显示一个空白选项,将我重定向到未定义的页面。我该如何解决?
- django - Django API 没有返回 URL 中带有端口的 URL,因此链接被破坏。如何包含端口?
- android - “Musixmatch”如何知道 YouTube 正在播放什么?
- calendar - 如何使用 powerapps 批量添加日历事件
- c - printf 没有按预期工作。有人可以解释输出吗?
- javascript - 如何将两个 fetch 请求合并到同一个数组中?
- sql - oracle sql 中的 COALESCE 未按预期运行
- listview - 颤振 | 在 ListView 中使用 Listview.builder - 使整个页面可滚动