简体   繁体   English

在调用该函数之前,该如何定义它?

[英]How can I define this function before I call it?

I am a student working on a small homework project that I need help with. 我是一名学生,正在从事一项需要帮助的小型家庭作业项目。 I am making a small turn-based game in which I have 2 problems. 我正在制作一个小型的回合制游戏,其中有两个问题。

The game's code: 游戏代码:

#include<iostream.h>

class player
{
public:
    int health;
    char name[15];
    int atk;
    int mgatk;
    int agi;
    int def;
    int mgdef;
    int turnvalid;
    int type;
    int ctr;

    void turnend(player, player);
    void attack(player, player);
    void block(player, player);
    void menu(player, player);
};

void turnend(player p1, player p2)
{
    if(p1.turnvalid == 1)
    {
        menu(p1, p2);
    }
    else
    {
        menu(p2, p1);
    }
}

void attack(player p1, player p2)
{
    if(p1.turnvalid == 1)
        if(p1.type == 1)
        {
            p2.health = p2.health - (p1.atk/p2.def)*100;
        }
        else if(p1.type == 2)
        {
            p2.health = p2.health - (p1.mgatk/p2.mgdef)*100;
        }
    p1.turnvalid = 0;
    turnend(p1, p2);
}

void block(player p1, player p2)
{
    if(p1.type == 1)
    {
        p2.health = p2.health - (p1.atk/p2.def)*50;
    }
    else if(p1.type == 2)
    {
        p2.health = p2.health - (p1.mgatk/p2.mgdef)*50;
    }
    p1.turnvalid = 0;
    turnend(p1, p2);
}

void menu(player p1, player p2)
{
    int ch;
    cout<< "What will you do? \n1.Attack\n2.Block\n3.Counter";
    cin >> ch;
    switch(ch)
    {
    case '1':
        attack(p1, p2);
        break;  
    case '2':
        block(p1, p2);
        break;
    case '3':
    default:
        {
            cout<< "\nWrong choice! Enter again...\n";
            menu(p1, p2);
        }
    }
}

// this is not a part I am currently using because I can't
// figure out how to make it work
void counter(player p1, player p2)
{
    cout<< "Player " << p1.name << "countered!";
}

void main()
{
    cout<<"Beginning the game! Know your options!\nAttack to deal damage.\n"
          "Block to reduce damage taken.\nCounter to take damage, then deal "
          "twice the damage you took on the same turn.";
}

In this turn-based game, players have 2 choices right now: attack or block. 在这个基于回合的游戏中,玩家现在有2个选择:攻击或格挡。 They run fine, but the problem comes up when I redirect them to the turn menu again. 它们运行正常,但是当我再次将它们重定向到转弯菜单时出现了问题。 Whenever I call that function, it says: [Error] c:\\users\\vive\\desktop\\not games\\utilities\\c++ programs\\game2.cpp:27: E2268 Call to undefined function 'menu' in function turnend(player,player) 每当我调用该函数时,它都会显示: [错误] c:\\ users \\ vive \\ desktop \\ not games \\ utilities \\ c ++ program \\ game2.cpp:27:E2268调用函数turnend(player,player中的未定义函数'menu' )

this probably happens because I defined turnend before I defined menu. 这可能是因为我在定义菜单之前先定义了转折率。 But if I define menu before turnend, this happens: 但是,如果我在转折点之前定义菜单,则会发生这种情况:

[Error] c:\\users\\madhav\\desktop\\not games\\utilities\\c++ programs\\game2.cpp:30: E2268 Call to undefined function 'attack' in function menu(player,player) [错误] c:\\ users \\ madhav \\ desktop \\ not games \\ utilities \\ c ++ program \\ game2.cpp:30:E2268调用功能菜单(播放器,播放器)中未定义的“攻击”功能

[Error] c:\\users\\madhav\\desktop\\not games\\utilities\\c++ programs\\game2.cpp:34: E2268 Call to undefined function 'block' in function menu(player,player) [错误] c:\\ users \\ madhav \\ desktop \\ not games \\ utilities \\ c ++ programs \\ game2.cpp:34:E2268调用功能菜单(播放器,播放器)中未定义的功能“块”

I'm essentially trapped, and I don't know what to do. 我本质上被困了,我不知道该怎么办。 No matter which one I define first, I get an error. 不管我先定义哪个,都会出现错误。 I've declared them all in the class player itself, so why is this happening? 我已经在类播放器本身中声明了它们,为什么会这样呢? How can I fix it? 我该如何解决?

Also, if someone can tell me how I can make the counter work I would really appreciate it. 另外,如果有人可以告诉我如何进行计数器工作,我将不胜感激。 What I want is to deal double the damage taken, and make the counter go last. 我想要的是造成的伤害加倍,并使计数器倒数第二位。 This would allow the player doing to counter to take the damage first, then return double the damage. 这将使玩家进行反击时可以先承受伤害,然后返回两倍的伤害。 And as for the stats like atk, mgatk, etc., I will affix them to two different 'classes' of characters, which will be determined by the int variable 'type'. 至于atk,mgatk等统计信息,我将它们附加到两个不同的“类”字符上,这将由int变量“ type”确定。

Any kind of help, criticism, advice etc. on the program is really appreciated. 对该程序的任何形式的帮助,批评,建议等,我们都非常感谢。 Thanks in advance! 提前致谢!

You error is caused by missing player:: in front of function's name in definitions. 您的错误是由于定义中函数名称前面缺少player::引起的。 Simply replace 只需更换

void attack(player p1, player p2){
(...)

with

void player::attack(player p1, player p2){
(...)

and so on. 等等。

When you don't mark defined function as member of class by putting class name before function's name (or define it inside class) compiler will recognize it as completely different function. 如果您不通过将类名放在函数名之前(或在类内定义)将标记的函数标记为类成员,则编译器会将其识别为完全不同的函数。

But probably better for you will be learn about dividing code into header- and source- files for avoiding more complicated mistakes in future. 但是,可能会更好地为您学习如何将代码分为头文件和源文件,以避免将来出现更复杂的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM