3

So I'm teaching myself C++ and completed this project to test my knowledge of everything up to functions (I'm aware that some of this can be done easier with arrays but that's not the point here). It's basically a little glorified rock-paper-scissors turn-based combat game, and I have collapsed the less relevant functions below.

Anyway, the original worked fine but combat_main() was very bloated (containing the whole combat_resolve), and I wanted to try to separate that out into its own function. What was keeping me from doing that was the knowledge that functions can only return one variable, but then I read about passing by reference, which should allow combat_resolve to modify the value of the arguments passed to it by combat_main.

However, this doesn't seem to be working for me. I don't know if I'm simply using the wrong syntax but it seems as soon as I try to pass-by-reference the function prototype starts conflicting with the function definition due to overloading. Any suggestions?

Code:

#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) {...}

Compilation errors:

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)
1
  • I assume you just stripped the rest of the code. But at the first glance - you are declaration and definition of combat_resolve don't match. Fix the declaration.
    – lapk
    Sep 17, 2013 at 20:51

2 Answers 2

2

The problem is that you define combate_resolve as passing only by value:

At begins:

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);

and at end (assuming that your are defining here) are passing some values by reference:

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)
1
  • Thanks. I didn't realize that merely declaring the parameter types in a prototype indicates a pass-by-value by default. Good to know.
    – mcman
    Sep 17, 2013 at 20:56
1

Below you have duplicated most of the function prototypes (but without argument names).

There's a mismatch for copmbat_resolve:

int combat_resolve(int hero_action,int nemesis_action,int &hero_health,int &hero_energy,int &nemesis_health,int &nemesis_energy);

vs.

int combat_resolve(int,int,int,int,int,int);

Note the missing & for the reference arguments. See it compiling on IdeOne:

int combat_resolve(int,int,int&,int&,int&,int&);  // FIX

Also, see the entry What is an undefined reference/unresolved external symbol error and how do I fix it? on general guidance on 'Unresolved Externals'

1
  • Awesome, thanks a lot. I didn't realize that you had to- or know the syntax to- indicate a pass-by-reference in the prototype. Seems to work fine now.
    – mcman
    Sep 17, 2013 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.