函数原型由于过载而干扰按引用传递
Function prototype interfering with pass-by-reference due to overloading?
所以我正在自学C++并完成了这个项目来测试我对函数的所有知识(我知道其中一些可以用数组更容易完成,但这不是这里的重点)。它基本上是一个有点美化的石头剪刀布回合制战斗游戏,我已经折叠了下面不太相关的功能。
无论如何,原版工作正常,但combat_main()非常臃肿(包含整个combat_resolve),我想尝试将其分离到自己的函数中。阻止我这样做的是函数只能返回一个变量的知识,但随后我读到了通过引用传递的信息,这应该允许combat_resolve修改combat_main传递给它的参数的值。
但是,这似乎对我不起作用。我不知道我是否只是使用了错误的语法,但似乎一旦我尝试通过引用传递,函数原型就会由于过载而开始与函数定义冲突。有什么建议吗?
法典:
#include <iostream>
using namespace std;
bool main();
int main_menu();
bool rules();
int combat_main();
int combat_hero(int,int,int,int);
int combat_nemesis(int,int,int,int);
int combat_resolve(int,int,int,int,int,int);
int combat_end(int,int);
bool main() {...}
int main_menu() {...}
bool rules() {...}
int combat_main() {
int play_again=0;
do {
bool active_battle=1;
cout << "Let the battle begin! Show no mercy!n" << 'n';
int round_number=0;
int hero_health=5;
int hero_energy=2;
int nemesis_health=5;
int nemesis_energy=2;
do {
if((hero_health<1)||(nemesis_health<1)) {
active_battle=0;
}
//recognizes that battle has ended
else {
++round_number;
cout << "Round " << round_number << 'n' << 'n';
int hero_action= combat_hero(hero_health,hero_energy,nemesis_health,nemesis_energy);
//receives player's choice of action
int nemesis_action= combat_nemesis(hero_health,hero_energy,nemesis_health,nemesis_energy);
//decides AI's choice of action
combat_resolve(hero_action,nemesis_action,hero_health,hero_energy,nemesis_health,nemesis_energy);
//decides round outcome from hero_action and nemesis_action
if(hero_action==0) {
hero_health=0;
//automatically ends battle if player opts to concede defeat
}
}
}
while(active_battle==1);
//cycles combat rounds until one opponent loses
play_again=combat_end(hero_health,nemesis_health);
//prompts player with option to play again at end of battle
}
while(play_again==1);
//repeats if "play again" selected after battle ends
return play_again;
}
int combat_hero(int hero_health,int hero_energy,int nemesis_health,int nemesis_energy) {...}
int combat_nemesis(int hero_health,int hero_energy,int nemesis_health,int nemesis_energy) {...}
int combat_resolve(int hero_action,int nemesis_action,int &hero_health,int &hero_energy,int &nemesis_health,int &nemesis_energy) {...}
int combat_end(int hero_health,int nemesis_health) {...}
编译错误:
1 error LNK2001: unresolved external symbol "int __cdecl combat_resolve(int,int,int,int,int,int)" (?combat_resolve@@YAHHHHHHH@Z)
2 error LNK1120: 1 unresolved externals
3 IntelliSense: more than one instance of overloaded function "combat_resolve" matches the argument list:
function "combat_resolve(int, int, int, int, int, int)"
function "combat_resolve(int hero_action, int nemesis_action, int &hero_health, int &hero_energy, int &nemesis_health, int &nemesis_energy)"
argument types are: (int, int, int, int, int, int)
问题是您将combate_resolve定义为仅按值传递:
在开始时:
int combat_main();
int combat_hero(int,int,int,int);
int combat_nemesis(int,int,int,int);
int combat_resolve(int,int,int,int,int,int); // <-- passing by value
int combat_end(int,int);
最后(假设您在此处定义)通过引用传递一些值:
int combat_hero(int hero_health,int hero_energy,int nemesis_health,int nemesis_energy)
int combat_nemesis(int hero_health,int hero_energy,int nemesis_health,int nemesis_energy)
int combat_resolve(int hero_action,int nemesis_action,int &hero_health,int &hero_energy,int &nemesis_health,int &nemesis_energy) // <-- passing some parameter as reference
int combat_end(int hero_health,int nemesis_health)
下面你复制了大部分函数原型(但没有参数名称)。
copmbat_resolve
不匹配:
int combat_resolve(int hero_action,int nemesis_action,int &hero_health,int &hero_energy,int &nemesis_health,int &nemesis_energy);
与。
int combat_resolve(int,int,int,int,int,int);
请注意引用参数缺少&
。在 IdeOne 上看到它的编译:
int combat_resolve(int,int,int&,int&,int&,int&); // FIX
另请参阅 c++-FAQ 条目 什么是未定义的引用/未解析的外部符号错误以及如何修复它? 关于"未解决的外部问题"的一般指南
相关文章:
- 按值传递变量与按引用传递变量具有相同的结果
- 获取 std::函数以推断按引用传递/按值传递
- C++/11 auto 关键字是在更有效时推导参数进行按引用传递,还是始终按值传递?
- 使用 enable_if 在按值传递与按引用传递之间更改函数声明
- 按引用传递和按地址传递之间的差异
- C++按引用传递还是按值传递?
- 可移动但不可复制的对象:按值传递还是按引用传递?
- 使用增量运算符按引用传递
- C++函数,按引用传递
- 按引用传递和动态内存分配之间的区别是什么
- 通过查看程序集来比较按值传递与按引用传递性能
- 在形式参数列表中自动使用 const 和按引用传递的任何明显后果
- 按引用传递函数参数
- C++按引用传递:如何使用调用堆栈
- 为什么按值传递 int 比按引用传递更快
- 是使用按引用传递来填充容器更好,还是使用返回值来填充容器更好
- 为什么 std::bind 在使用按引用传递时防止后期绑定?
- 与 C++ 相比,C# 中的按引用传递的工作方式是否正确?
- C++如何使用按值传递和按引用传递的重载方法
- 函数原型由于过载而干扰按引用传递