簡體   English   中英

為什么我的IDE告訴我在Squirtle.cpp構造函數中未定義“ newHp”?

[英]Why does my IDE tell me that “newHp” is undefined in my Squirtle.cpp constructor?

我是C ++的新手,正在練習繼承,並在下面鍵入了代碼。 在Squirtle.cpp中,Visual Studio告訴我newHp,newLevel,newExperience和newAttack均未定義。 為什么會這樣呢? 我如何解決它? 我抬頭一看這里,如其他例子這個 ,但我猜是因為他們都基地和孩子構造函數是在同一個文件,他們我就不會得到一個錯誤?

****口袋妖怪.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

****口袋妖怪.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!";
}

在此構造函數定義中

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

未聲明標識符newHp,newLevel,newExperience,newAttack。 因此,編譯器不知道例如什么是newHp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM