所以我最近决定再次选择编程并使用C ++。试图做一个冒险家课,但我似乎遇到了一些麻烦。这是我的文件:
Adventurer.h:
#ifndef __Adventurer_H_INCLUDED__ //if Adventurer.h hasn't been included yet...
#define __Adventurer_H_INCLUDED__ //#define this so the compiler knows it has been included
class Adventurer
{
private:
int hp, mp, str, agi, magic, armour;
public:
Adventurer(){}
void printStats();
}
#endif
Adventurer.cpp:
#include <iostream>
#include "Adventurer.h"
Adventurer::Adventurer()
{
hp = 50;
mp = 25;
str = 5;
agi = 5;
magic = 5;
armour = 5;
}
void Adventurer::printStats()
{
cout << "HP = " << hp << "\n\n";
cout << "MP = " << mp << "\n\n";
cout << "str = " << str << "\n\n";
cout << "agi = " << agi << "\n\n";
cout << "magic = " << magic << "\n\n";
cout << "armour = " << armour << "\n\n";
}
RPG_Game.cpp:
// my first program in C++
#include <iostream>
#include <string>
#include "Adventurer.h"
;using namespace std;
int main()
{
cout << "Hello Adventurer! What is your name? \n";
string advName;
cin >> advName;
cout << "\nYour name is " << advName << "!";
Adventurer *adv = new Adventurer();
cout << adv.printStats();
delete adv;
system(pause);
}
答案 0 :(得分:0)
让我们看一下代码中的错误
首先,在您的Adventurer.h中,在课后添加分号(;
)。
接下来,在同一个班级,你有
Adventurer(){}
将此更改为
Adventurer();
然后,在您的RPG_Game.cpp中,更改
cout << adv.printStats();
到
adv->printStats() ;
使用指针时,您需要使用->
而不是.
最后,
system(pause);
应该是
system( "pause" );
现在,尝试运行您的代码。
另外,您可能会发现this有帮助。