3

そこで、私は C++ を独学し、このプロジェクトを完了して、関数までのすべての知識をテストしました (これの一部は配列で簡単に実行できることを認識していますが、それはここでのポイントではありません)。これは基本的に、ちょっと美化されたじゃんけんターンベースの戦闘ゲームであり、関連性の低い機能を以下にまとめました。

とにかく、オリジナルは問題なく動作しましたが、combat_main() は非常に肥大化していた (combat_resolve 全体を含む) ため、それを独自の関数に分離しようとしました。私がそうするのを妨げていたのは、関数が 1 つの変数しか返せないという知識でしたが、参照渡しについて読みました。これにより、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)
4

2 に答える 2

2

問題は、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)
于 2013-09-17T20:51:07.610 に答える
1

以下では、ほとんどの関数プロトタイプを複製しています (ただし、引数名はありません)。

の不一致があります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

また、エントリ未定義の参照/未解決の外部シンボル エラーとは何ですか? どうすれば修正できますか? も参照してください。「未解決の外部」に関する一般的なガイダンスについて

于 2013-09-17T20:50:19.927 に答える