V1V3
V1V3

Reputation: 37

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

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)

[Error] c:\users\madhav\desktop\not games\utilities\c++ programs\game2.cpp:34: E2268 Call to undefined function 'block' in function menu(player,player)

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'.

Any kind of help, criticism, advice etc. on the program is really appreciated. Thanks in advance!

Upvotes: 0

Views: 97

Answers (1)

Jadeszek
Jadeszek

Reputation: 64

You error is caused by missing player:: in front of function's name in definitions. 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.

Upvotes: 3

Related Questions