私は単純なポケモンのテキストゲームをC++で作ろうとしていました。 私はpokemonのためのクラスを作成し、次に私のpokemain.cppに、charmanderからhpを出力しようとしました。 pokemonmain.cppを実行しようとすると、charmanderが宣言されていないと表示されます。これは間違った質問だと確信していますが、私はそれに答えを見つけることはできません。クラスから.cppへ
ここに私のコードです。
//class named stats
#include <iostream>
using namespace std;
class pokemon
{
int health, damage;
public:
pokemon (int,int);
int hp()
{
return (health);
}
int dmg()
{
return (damage);
}
};
pokemon::pokemon (int hp, int dmg)
{
health = hp;
damage = dmg;
pokemon charmander (25,3);
pokemon bulbasaur (20,4);
pokemon squirtle (30,2);
cout<<" Charmander has "<<charmander.hp()<<" hp and "<<charmander.dmg()<<" damage.\n";
cout<<" Bulbasaur has "<<bulbasaur.hp()<<" hp and "<<bulbasaur.dmg()<<" damage.\n";
cout<<" Squirtle has "<<squirtle.hp()<<" hp and "<<squirtle.dmg()<<" damage.\n";
}
//pokemain.cpp
#include <iostream>
#include "stats.h"
using namespace std;
int main()
{
cout<<charmander.hp();
return 0;
}
あなたの返事にも感謝します。あなたはどちらも私に多くの研究を救いました – Chuy
問題ありません。どのコンストラクタが何であるかを調べることをお勧めします。私はあなたが概念を完全に把握していないと言うことができます。あなたはここから始めることができます:http://www.fredosaurus.com/notes-cpp/oop-condestructors/constructors.html – Tiago
そのリンクのおかげで、私は現在、大学でC++になりました。彼女のアクセント。 – Chuy