我试图在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 2012-02-12 21:58:49
没问题。我建议你研究一下构造函数是什么。我可以告诉你没有完全理解这个概念。你可以从这里开始:http://www.fredosaurus.com/notes-cpp/oop-condestructors/constructors.html – Tiago 2012-02-12 22:06:42
谢谢你的链接,我目前在C++大学里,而且很难理解我的老师,因为她的口音。 – Chuy 2012-02-12 22:16:13