我几乎不了解Pure虚拟功能。我有一个纯虚函数virtual int createCharacter() = 0;
,我正在添加characterStrength,characterDexterity和characterIntelligence,它们是我班级中的私有成员。我知道我需要添加访问器和更改器,但我不知道如何。任何帮助是极大的赞赏。我试图在main中显示总返回,但是我收到了这些错误:
Human.h: In function âint main()â:
Human.h:12: error: âint Human::characterStrengthâ is private
maingame.cpp:48: error: within this context
Human.h:13: error: âint Human::characterDexterityâ is private
maingame.cpp:48: error: within this context
Human.h:12: error: âint Human::characterStrengthâ is private
maingame.cpp:48: error: within this context
Human.h:14: error: âint Human::characterIntelligenceâ is private
maingame.cpp:48: error: within this context
Human.h:15: error: âstd::string Human::characterTypeâ is private
maingame.cpp:50: error: within this context
Human.h:16: error: âint Human::characterTotalâ is private
maingame.cpp:50: error: within this context
这是我的代码:
#include "Human.h"
#include "Orc.h"
using namespace std;
//Main.cpp
int main()
{
//Character cc;
Human H;
Orc O;
char choice;
char userC;
cout << "Welcome!\n";
cout << "" << endl;
cout << "Pick your choice:\n";
cout << "A -- Human\n";
cout << "B -- Orc\n";
cin >> choice;
switch (choice)
{
case 'a':
case 'A':
H.getType();
H.getStrength();
H.getDexterity();
H.getIntelligence();
H.createCharacter();
if (H.characterStrength > H.characterDexterity && H.characterStrength > H.characterIntelligence)
{
cout << "The strong " << H.characterType << " has a total score of " << H.characterTotal << endl;
}
}
return 0;
}
//human.h
#ifndef HUMAN_H
#define HUMAN_H
#include "Character.h"
using namespace std;
class Human : public Character
{
private:
int characterStrength;
int characterDexterity;
int characterIntelligence;
string characterType;
int characterTotal;
public:
Human();//Constructor
~Human();
int getStrength ()
{
cout << "Strength : Enter a number from 0 to 18\n";
cin >> characterStrength;
return characterStrength;
}
int getDexterity ()
{
cout << "Dexterity : Enter a number from 0 to 18\n";
cin >> characterDexterity;
return characterDexterity;
}
int getIntelligence ()
{
cout << "Intelligence : Enter a number from 0 to 18\n";
cin >> characterIntelligence;
return characterIntelligence;
}
string getType ()
{
cout << "Please choose one of the following\n";
cout << "A -- Paladin \n";
cout << "B -- Ranger \n";
cout << "C -- Wizard \n";\
cin >> characterType;
return characterType;
}
virtual int createCharacter()
{
characterTotal = characterStrength + characterIntelligence + characterDexterity;
return characterTotal;
}
};
#endif
//Character.h
#ifndef CHARACTER_H
#define CHARACTER_H
#include <iostream>
#include <string>
using namespace std;
class Character
{
protected:
float characterTotal;
public:
virtual int createCharacter() = 0; //Pure virtual function
Character();
~Character();
};
#endif
答案 0 :(得分:1)
好。编译器消息非常简单。您正在尝试访问类外的私有成员,这是禁止的。让我们尝试一些简单的例子来了解事情:
class Character
{
public:
virtual std::string GetClass() = 0;
int GetHP() {return(m_nHP);};
int GetMP() {return(m_nMP);};
//You must declare virtual destructor!
virtual ~Character() {};
protected:
int m_nHP, m_nMP;
};
class Warrior : public Character
{
public:
Warrior() : m_nHP(100), m_nMP(0)
std::string GetClass(){ return("Warrior"); };
};
class Mage : public Character
{
public:
Mage() : m_nHP(30), m_nMP(100)
std::string GetClass(){ return("Mage"); };
};
int main(void)
{
Character* pChar = new Mage();
std::cout << pChar->GetClass()<<pChar->GetHP()<<pChar->GetMP();
delete pChar;
return(0);
}