0

I'm new to C++ and I'm practicing inheritance and I typed the code below. In Squirtle.cpp, visual studio is telling me that newHp, newLevel, newExperience and newAttack are all undefined. Why is this the case? How do I fix it? I've looked up other examples here such as this but I guess they would't get an error because both their base and child constructors are in the same file?

****Pokemon.h****

#ifndef _POKEMON_
#define _POKEMON_
#include <string>
#include <iostream>
using namespace std;

class Pokemon {
//Members
private:
    int hp;
    int level;
    int experience;
    int attack;
//Member functions
public:
    Pokemon(int newHp, int newLevel, int newExperience, int newAttack);
    virtual ~Pokemon();
    int getHp();
    int getAttack();
    void setHp(int newHp);
    void setAttack(int newAttack);
    void physicalAttack(Pokemon &target);
};

#endif

****Pokemon.cpp****

#include "Pokemon.h"


Pokemon::Pokemon(int newHp, int newLevel, int newExperience, int newAttack)
{
    hp = newHp;
    level = newLevel;
    experience = newExperience;
    attack = newAttack;

}

Pokemon::~Pokemon()
{
}

int Pokemon::getHp()
{
    return hp;
}

int Pokemon::getAttack()
{
    return attack;
}

void Pokemon::setHp(int newHp)
{
    hp = newHp;
}

void Pokemon::setAttack(int newAttack)
{
    attack = newAttack;
}

void Pokemon::physicalAttack(Pokemon &target)
{
    target.setHp(target.getHp() - getAttack());
    cout << "Dealt " << getAttack() << "damages to target! Hp is now at " << target.getHp() << "!";
}

****Squirtle.h****

#include "Pokemon.h"
#ifndef _SQUIRTLE_
#define _SQUIRTLE_
class Squirtle :Pokemon{
//members
private:
    int mana;
//member functions
public:
    Squirtle(int newMana);
    int getMana();
    void setMana(int newMana);
    void freeze(Pokemon &target);
};

#endif

****Squirtle.cpp****

#include "Squirtle.h"

Squirtle::Squirtle(int newMana):Pokemon(newHp, newLevel, newExperience, newAttack)
{
    mana = newMana;
}

int Squirtle::getMana()
{
    return mana;
}

void Squirtle::setMana(int newMana)
{
    mana = newMana;
}

void Squirtle::freeze(Pokemon &target)
{
    setMana(getMana() - 1);
    target.setAttack(0);
    cout << "Squirtle has frozen the target! Its attack is now reduced to 0!";
}
Community
  • 1
  • 1
user2997154
  • 415
  • 7
  • 18
  • Do you really have to post so much code, and drawn out over so much space to boot? Also, FYI, four stars don't mean anything to markdown here, use one less. – Deduplicator Nov 07 '14 at 04:43
  • Your include guard is a [reserved identifier](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – chris Nov 07 '14 at 04:48

1 Answers1

0

In this constructor definition

Squirtle::Squirtle(int newMana):Pokemon(newHp, newLevel, newExperience, newAttack)
{
    mana = newMana;
}

identifiers newHp, newLevel, newExperience, newAttack are not declared. So the compiler does not know for example what is newHp

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Writting "...:Pokemon(int newHp, int newLevel, ..." also gives me an error so do I really need to define the variables of the base constructor every time I reference it if the base constructor is in a different file? – user2997154 Nov 07 '14 at 05:22
  • 2
    @user2997154 You need to treat that piece like calling a function, not declaring a function - since that's what it's doing: calling the constructor of Pokemon. You've written that constructor to take in 4 values. Now you have to figure out where they come from. Will they be hardcoded in that line? Should they be parameters of the Squirtle constructor that get passed down? Right now they're not coming from anywhere because those variables haven't been declared - that's why the compiler is complaining. – TheUndeadFish Nov 07 '14 at 05:30