C++ Game Error.

  • (7 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

94 Replies - 3569 Views - Last Post: 12 November 2017 - 01:22 AM Rate Topic: -----

#1 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 10-December 16

C++ Game Error.

Posted 14 October 2017 - 08:49 AM

I am trying to pass a variable to a class, but I am getting an error. The error is located on the 58th line in Enemy.cpp Could you please help? Thanks!
Error: no matching function for call to 'Player::Player()'

main.cpp
#include "Player.h"
#include "Enemy.h"
#include "Animations.h"
#include "resource.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

void menu(int game_over, string player_name);
void menu_dank(int game_over, string player_name);
void enemyCalling();
void Death();

int main()
{
    string player_name;
    int Tile = 1;
    bool enemyEncountered;

    int playerHealth = 20;
    int enemyHealth = 20;

    cout<<"                          Welcome to MortalKombat 2.\n\n"<<endl;
    cout<<"                          Press any key to continue.\n"<<endl;
    cout<<"--------------------------------------------------------------------------------"<<endl;
    cout<<"\n\n Made By: Xrey274\n"<<endl;
    cout<<" Running Version: Alpha 1.4\n"<<endl;
    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;
    cout<<"                                  Update Info"<<endl;

    if(cin.get())
    {
        system("cls");
    }

    cout<<"                            Please enter your name:\n\n"<<endl;
    cout<<"                                   ";cin>>player_name;

    bool game_over = false;
    Player pl(playerHealth, enemyHealth);

    while(!game_over)
    {
        system("cls");
        if(Tile == 69)
        {
            menu_dank(game_over, player_name);
        }
        else
        {
            menu(game_over, player_name);
        }


        int input;
        cin>>input;

        switch(input)
        {
            case 1:
                system("cls");
                pl.playerMovement(Tile, enemyEncountered);
                break;
            case 2:
                system("cls");
                pl.playerAttack(player_name, enemyEncountered);
                break;
        }
    }

    if(game_over)
    {
        Death();
    }
}

void menu(int game_over, string player_name)
{
    cout<<"                      Please choose what you want to do.\n"<<endl;
    cout<<"--------------------------------------------------------------------------------"<<endl;

    cout<<"\n 1-Move\n"<<endl;
    cout<<" 2-Attack\n"<<endl;
    cout<<" 3-Inventory\n"<<endl;
    cout<<" 4-Stats\n"<<endl;
    cout<<" Player Name:"<<player_name<<endl;

    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;
}

void menu_dank(int game_over, string player_name)
{
    cout<<"                      69696969696969696969696969696969696969.\n"<<endl;
    cout<<"--------------------------------------------------------------------------------"<<endl;

    cout<<"\n 1-6969\n"<<endl;
    cout<<" 2-696969\n"<<endl;
    cout<<" 3-6969696969\n"<<endl;
    cout<<" 4-69696969\n"<<endl;
    cout<<" 69696969:"<<player_name<<endl;

    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;
}

void Death()
{

}




Player.cpp
#include "Player.h"
#include "Enemy.h"
#include "Animations.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

Player::Player(int playerHealth, int enemyHealth)
{
    playerHealth = 20;
    enemyHealth = 20;
}

void Player::playerMovement(int& Tile, bool& enemyEncountered)
{
    cout<<"                      How many tiles do you want to move?"<<endl;
    cout<<"\n\n                                    Tile:"<<Tile<<"\n"<<endl;
    cout<<"\n--------------------------------------------------------------------------------"<<endl;
    cout<<"\n\n                    Hint:You can only move 5 tile at a time."<<endl;

    if(enemyEncountered == false)
    {
        cin>>movementInput;

        Enemy en;

        int x = 6 - movementInput;

        for(int i = x; i <= 5; i++)
        {
            Tile++;
            en.enemyEncountering(1, enemyEncountered);
        }
    }

    else if(enemyEncountered == true)
    {
        cout<<"\n                  Sorry, but you can't move while in a fight!"<<endl;

        cin.ignore();
        cin.get();
    }

}

void Player::playerAttack(string player_name, bool& enemyEncountered)
{
    cout<<"                              Please choose an attack!"<<endl;

    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;

    cout<<" 1.Hadouken\n"<<endl;
    cout<<" 2.Recover\n"<<endl;
    cout<<" 3.Might Guy\n"<<endl;
    cout<<" 4.Toxic Spikes"<<endl;

    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;

    cout<<" "<<player_name<<"'s Health"<<endl;

    cin>>attackInput;

    Enemy en;
    Animations an;
    switch(attackInput)
    {
        case 1:
            take_eDamage(5);
            an.hadouken_Animation();
            break;

    }
}

void Player::take_pDamage(int pDamage)
{
    int x = 6 - pDamage;

    for(int y = x; y <= 5; y++)
    {
        playerHealth = playerHealth - 1;

    }
}

void Player::take_eDamage(int eDamage)
{
    int x = 6 - eDamage;

    for(int y = x; y <= 5; y++)
    {
        enemyHealth = enemyHealth - 1;

        cout<<enemyHealth;

        cin.ignore();
        cin.get();
    }
}

void Player::enemyHealthLeft()
{
    cout<<"\n Enemy's Health: "<<enemyHealth<<endl;

    cin.ignore();
    cin.get();
    system("cls");
}




Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <string>

using namespace std;

class Player
{
    public:
        Player(int, int);
        void playerMovement(int&, bool&);
        void playerAttack(string, bool&);
        void take_pDamage(int);
        void take_eDamage(int);
        void enemyHealthLeft();
    private:
        int movementInput;
        int attackInput;
        int playerHealth;
};

#endif // PLAYER_H




Enemy.cpp
#include "Player.h"
#include "Enemy.h"
#include "Animations.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

Enemy::Enemy()
{

}

void Enemy::enemyEncountering(int tilesMoved, bool& enemyEncountered)
{
    int encounter;

    srand(time(NULL));

    for(int x = 0; x <= 5; x++)
    {
        if(tilesMoved = 1)
        {
            encounter = ( rand()%5 + rand()%4 ) - rand()%3 - tilesMoved;
        }
        if(encounter == 1)
        {
            x = 6;

            system("cls");
            cout<<"                              Enemy encountered!"<<endl;
            enemyEncountered = true;

            cin.ignore();
            if(cin.get())
            {
                system("cls");
            }
        }
    }
}

void Enemy::enemyAttack(bool enemyEncountered)
{
    if(enemyEncountered == true)
    {
        srand(time(NULL));

        int attackChance;

        for(int y; y <= 5; y++)
        {
            attackChance = rand()%3;
        }

        Player pl;
        switch(attackChance)
        {
            case 1:
                pl.take_pDamage(1);
                break;
        }
    }
}


void Enemy::enemyDeath()
{

}




Enemy.h
#ifndef ENEMY_H
#define ENEMY_H


class Enemy
{
    public:
        Enemy();
        void enemyEncountering(int, bool&);
        void enemyAttack(bool);
        void enemyDeath();
    private:
        int enemyHealth;
};

#endif // ENEMY_H





Is This A Good Question/Topic? 0
  • +

Replies To: C++ Game Error.

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 5,381
  • Joined: 27-December 05

Re: C++ Game Error.

Posted 14 October 2017 - 09:28 AM

Your Player constructor (Player.h line 10) calls for 2 arguments but the function call on line 58 of Enemy.cpp has no arguments. Your Player constructor should probably not have any parameters since it is set up to assign default values to the new object and doesn't need any input from the user.

Some more info on default constructors.
Was This Post Helpful? 1
  • +
  • -

#3 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,466
  • Joined: 13-June 14

Re: C++ Game Error.

Posted 14 October 2017 - 09:31 AM

Line 58 in Enemy.cpp attempts to call the default (no argument) constructor of Player. When you provide constructors for your class (as you have on line 10 of Player.h and in the corresponding implementation file), the compiler doesn't provide the default constructor for you; it only does so when you don't provide any other constructor. This should be pointed out in your learning materials.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13482
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: C++ Game Error.

Posted 14 October 2017 - 09:31 AM

Does the 'enemy class' have an include to the player class? If not then it cannot instantiate a brand new player variable. Note.. this player variable is *NOT* the same as the one in the main. It is brand new memory. If you want it to be the same memory object then perhaps pass it into the function from the main.

58	        Player pl;
59	        switch(attackChance)
60	        {
61	            case 1:
62	                pl.take_pDamage(1);

Was This Post Helpful? 0
  • +
  • -

#5 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 10-December 16

Re: C++ Game Error.

Posted 14 October 2017 - 09:57 AM

I am trying to pass down 1 to take_pDamage, not to the constructor? Also i have 1 as an aurgument. Isn't it considerated a argument, even tho it isn't a variable?

Modi123_1 can't i just pass it from main to the constructor as a pointer?
Was This Post Helpful? 0
  • +
  • -

#6 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,466
  • Joined: 13-June 14

Re: C++ Game Error.

Posted 14 October 2017 - 10:25 AM

You're calling a constructor that doesn't exist for Player on line 58 (the default constructor). You're allowed to omit the parentheses when you're calling the default constructor, i.e. line 58 is equivalent to

Player p1();


See again the link in post 2 about default constructors.

This post has been edited by ndc85430: 14 October 2017 - 10:28 AM

Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 5,381
  • Joined: 27-December 05

Re: C++ Game Error.

Posted 14 October 2017 - 10:59 AM

View PostXrey274, on 14 October 2017 - 12:57 PM, said:

I am trying to pass down 1 to take_pDamage, not to the constructor? Also i have 1 as an aurgument. Isn't it considerated a argument, even tho it isn't a variable?

Yes, 1 is a valid argument to take_pDamage. That's not a problem.

But ...

View PostXrey274, on 14 October 2017 - 12:57 PM, said:

Modi123_1 can't i just pass it from main to the constructor as a pointer?

You can't pass the object from main to the constructor in Enemy.cpp in order to create the same object. If you want some class member function to work on some particular object (other than the "this" object), you have to pass a reference to that object as an argument to the member function. So, in your example, enemyAttack should have a reference or pointer to the Player object that's taking the damage as a parameter (and in the function call you would presumably send the Player object that was created in main as the argument).

(By the way, do you really need a separate Enemy class? Isn't an "enemy" simply the opponent's Player?)
Was This Post Helpful? 0
  • +
  • -

#8 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 10-December 16

Re: C++ Game Error.

Posted 14 October 2017 - 11:12 AM

So how should i fix it? Where should i put the playerHealth and enemyHealh variables?
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 5,381
  • Joined: 27-December 05

Re: C++ Game Error.

Posted 14 October 2017 - 11:26 AM

Well, I think the first question is, do you want each player to know the value of his enemy's health? If yes, then why not make them both member variables of the Player class? If no, then "enemyHealth" is just the "playerHealth" of the other Player and all you need is a playerHealth member variable.

If I understand correctly what you're trying to do, the "enemy" is just another Player. So if you have player1 and player2, then player2 is player1's enemy and vice versa. But all of the variables and all of the functions that act on Player objects should probably be members of the Player class.
Was This Post Helpful? 0
  • +
  • -

#10 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 10-December 16

Re: C++ Game Error.

Posted 14 October 2017 - 12:28 PM

I want them to see the enemy health. Think of the enemy as of an NPC. Every time you kill an enemy you encounter another one.
Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 5,381
  • Joined: 27-December 05

Re: C++ Game Error.

Posted 14 October 2017 - 12:38 PM

Ah, okay. So yeah it does make sense to have an Enemy class. So then if a Player is going to be modified by a member function of Enemy you have to send that Player (by pointer or reference) to the Enemy.

Player health clearly should be a member of Player. Enemy health should be a member of Enemy and you can make it visible to the Player by having a public getHealth member of Enemy.

Edit: And maybe there should be a getHealth member of Player as well, if the Enemy needs to know the Player's health level.

This post has been edited by r.stiltskin: 14 October 2017 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#12 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 10-December 16

Re: C++ Game Error.

Posted 15 October 2017 - 08:03 AM

Ok i changed the code, but it still doesn't work. Looks like you can't put variables in constructors. My biggest problem is where can should i put the variables playerHealth and enemyHealth.
Was This Post Helpful? 0
  • +
  • -

#13 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,466
  • Joined: 13-June 14

Re: C++ Game Error.

Posted 15 October 2017 - 08:12 AM

Of course you can put variables in constructors. They're just methods, albeit special ones for creating instances. You need to show your updated code and explain what it does now.
Was This Post Helpful? 0
  • +
  • -

#14 Xrey274  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 10-December 16

Re: C++ Game Error.

Posted 15 October 2017 - 08:19 AM

Basically i moved take_eDamage to Enemy.cpp and i put playerHealth in Player::Player() and enemyHealth to Enemy::Enemy(). When i print out the enemy health it says 0. It's also reseting to 0 after i attack.

main.cpp
#include "Player.h"
#include "Enemy.h"
#include "Animations.h"
#include "resource.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

void menu(int game_over, string player_name);
void menu_dank(int game_over, string player_name);
void enemyCalling();
void Death();

int main()
{
    string player_name;
    int Tile = 1;
    bool enemyEncountered;

    cout<<"                          Welcome to MortalKombat 2.\n\n"<<endl;
    cout<<"                          Press any key to continue.\n"<<endl;
    cout<<"--------------------------------------------------------------------------------"<<endl;
    cout<<"\n\n Made By: Xrey274\n"<<endl;
    cout<<" Running Version: Alpha 1.4\n"<<endl;
    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;
    cout<<"                                  Update Info"<<endl;

    if(cin.get())
    {
        system("cls");
    }

    cout<<"                            Please enter your name:\n\n"<<endl;
    cout<<"                                   ";cin>>player_name;

    bool game_over = false;
    Player pl;

    while(!game_over)
    {
        system("cls");
        if(Tile == 69)
        {
            menu_dank(game_over, player_name);
        }
        else
        {
            menu(game_over, player_name);
        }


        int input;
        cin>>input;

        switch(input)
        {
            case 1:
                system("cls");
                pl.playerMovement(Tile, enemyEncountered);
                break;
            case 2:
                system("cls");
                pl.playerAttack(player_name, enemyEncountered);
                break;
        }
    }

    if(game_over)
    {
        Death();
    }
}

void menu(int game_over, string player_name)
{
    cout<<"                      Please choose what you want to do.\n"<<endl;
    cout<<"--------------------------------------------------------------------------------"<<endl;

    cout<<"\n 1-Move\n"<<endl;
    cout<<" 2-Attack\n"<<endl;
    cout<<" 3-Inventory\n"<<endl;
    cout<<" 4-Stats\n"<<endl;
    cout<<" Player Name:"<<player_name<<endl;

    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;
}

void menu_dank(int game_over, string player_name)
{
    cout<<"                      69696969696969696969696969696969696969.\n"<<endl;
    cout<<"--------------------------------------------------------------------------------"<<endl;

    cout<<"\n 1-6969\n"<<endl;
    cout<<" 2-696969\n"<<endl;
    cout<<" 3-6969696969\n"<<endl;
    cout<<" 4-69696969\n"<<endl;
    cout<<" 69696969:"<<player_name<<endl;

    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;
}

void Death()
{

}




Player.cpp
#include "Player.h"
#include "Enemy.h"
#include "Animations.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

Player::Player()
{
    int playerHealth = 20;
}

void Player::redirecting()
{
    
}

void Player::playerMovement(int& Tile, bool& enemyEncountered)
{
    cout<<"                      How many tiles do you want to move?"<<endl;
    cout<<"\n\n                                    Tile:"<<Tile<<"\n"<<endl;
    cout<<"\n--------------------------------------------------------------------------------"<<endl;
    cout<<"\n\n                    Hint:You can only move 5 tile at a time."<<endl;

    if(enemyEncountered == false)
    {
        cin>>movementInput;

        Enemy en;

        int x = 6 - movementInput;

        for(int i = x; i <= 5; i++)
        {
            Tile++;
            en.enemyEncountering(1, enemyEncountered);
        }
    }

    else if(enemyEncountered == true)
    {
        cout<<"\n                  Sorry, but you can't move while in a fight!"<<endl;

        cin.ignore();
        cin.get();
    }

}

void Player::playerAttack(string player_name, bool& enemyEncountered)
{
    cout<<"                              Please choose an attack!"<<endl;

    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;

    cout<<" 1.Hadouken\n"<<endl;
    cout<<" 2.Recover\n"<<endl;
    cout<<" 3.Might Guy\n"<<endl;
    cout<<" 4.Toxic Spikes"<<endl;
    Enemy en;
    en.getHealth();

    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;

    cout<<" "<<player_name<<"'s Health"<<endl;

    cin>>attackInput;

    Animations an;
    switch(attackInput)
    {
        case 1:
            en.take_eDamage(5);
            an.hadouken_Animation();
            break;

    }
}

void Player::take_pDamage(int pDamage)
{
    int x = 6 - pDamage;

    for(int y = x; y <= 5; y++)
    {
        playerHealth = playerHealth - 1;

    }
}



void Player::enemyHealthLeft()
{
    Enemy en;

    cout<<"\n Enemy's Health: "; en.getHealth();

    cin.ignore();
    cin.get();
    system("cls");
}




Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <string>

using namespace std;

class Player
{
    public:
        Player();
        void redirecting();
        void playerMovement(int&, bool&);
        void playerAttack(string, bool&);
        void take_pDamage(int);
        void enemyHealthLeft();
    private:
        int movementInput;
        int attackInput;
        int playerHealth;
};

#endif // PLAYER_H




Enemy.cpp
#include "Player.h"
#include "Enemy.h"
#include "Animations.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>

using namespace std;

Enemy::Enemy()
{
    int enemyHealth = 20;
}

void Enemy::enemyEncountering(int tilesMoved, bool& enemyEncountered)
{
    int encounter;

    srand(time(NULL));

    for(int x = 0; x <= 5; x++)
    {
        if(tilesMoved = 1)
        {
            encounter = ( rand()%5 + rand()%4 ) - rand()%3 - tilesMoved;
        }
        if(encounter == 1)
        {
            x = 6;

            system("cls");
            cout<<"                              Enemy encountered!"<<endl;
            enemyEncountered = true;

            cin.ignore();
            if(cin.get())
            {
                system("cls");
            }
        }
    }
}

void Enemy::enemyAttack(bool enemyEncountered)
{
    if(enemyEncountered == true)
    {
        srand(time(NULL));

        int attackChance;

        for(int y; y <= 5; y++)
        {
            attackChance = rand()%3;
        }

        Player pl;
        switch(attackChance)
        {
            case 1:
                pl.take_pDamage(1);
                break;
        }
    }
}

void Enemy::take_eDamage(int eDamage)
{
    int x = 6 - eDamage;

    for(int y = x; y <= 5; y++)
    {
        enemyHealth = enemyHealth - 1;

        getHealth();
    }

    cin.ignore();
    cin.get();
}

void Enemy::getHealth()
{
    cout<<enemyHealth<<endl;
}

void Enemy::enemyDeath()
{

}




Enemy.h
#ifndef ENEMY_H
#define ENEMY_H


class Enemy
{
    public:
        Enemy();
        void enemyEncountering(int, bool&);
        void enemyAttack(bool);
        void take_eDamage(int);
        void getHealth();
        void enemyDeath();
    private:
        int enemyHealth;
};

#endif // ENEMY_H



Was This Post Helpful? 0
  • +
  • -

#15 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 587
  • View blog
  • Posts: 2,466
  • Joined: 13-June 14

Re: C++ Game Error.

Posted 15 October 2017 - 08:27 AM

Look at line 13 of Player.cpp: you're declaring and initialising a local variable called playerHealth that then goes out of scope when the constructor returns. You aren't assigning to the member variable of the instance - you can see that because you're including a type, which means that this variable called playerHealth is shadowing the member variable.
Was This Post Helpful? 1
  • +
  • -

  • (7 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »