大量的错误与杂乱的OOP代码有关
A ton of errors related to messy OOP code
当我编译时,我得到加载的错误。希望事情会变得更清楚,如果我解决其中一个(如果不是,然后我可以张贴其余的):
'Weapon' : illegal member initialization: 'Name' is not a base or member
它有Name和Cost。武器继承可购物物品,可购物物品的保护部分有名称、价格和描述。
Shopable.h:
#ifndef _SHOPABLE_H_
#define _SHOPABLE_H_
#include "Library.h"
class Shopable{
protected:
std::string Name;
int Cost;
std::string Description;
public:
std::string getName() const{return Name;}
int getCost() const {return Cost;}
virtual std::string getDesc() const = 0;
};
#endif
Weapon.h:
#ifndef _WEAPON_H_
#define _WEAPON_H_
#include "Shopable.h"
class Weapon : public Shopable{
private:
int Damage;
public:
Weapon(int c,int d,std::string n) : Cost(c), Damage(d), Name(n){}
std::string getDesc() const{
return getName()+"t"+tostring(Damage)+"t"+tostring(Cost);
}
int getDamage() const{return Damage;}
int DamageTarget(Entity* target){
int DamageDealt = 0;
//do damage algorithm things here
return DamageDealt;
}
};
#endif
Library.h:
#ifndef _LIBRARY_
#define _LIBRARY_
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdarg>
#include <vector>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <map>
#include <exception>
#include <sstream>
//file includes
#include "Globals.h"
#include "Player.h"
#include "Exception.h"
#include "Weapon.h"
#include "Armour.h"
#include "Consumable.h"
//prototypes that require "Library.h"
bool Poglathon(std::vector<std::string>& text,Player *player);
bool PoglathonTown(std::vector<std::string>& text,Player *player);
std::map<std::string,Weapon*> init_weapons(void);
std::map<std::string,Armour*> init_armour(void);
std::map<std::string,Consumable*> init_consumables(void);
#endif //__LIBRARY__
上面:
//global variables
#ifndef _GLOBAL_
#define _GLOBAL_
#include <vector>
#include <iostream>
#include <string>
//prototypes
void NPCTalk(std::string const& speaker,std::vector<std::string> const& text);
void wait(double seconds);
void regionChange(int amount);
int getPositionInStringVector(std::vector<std::string> const& vec,std::string value);
//variables
//defines
#define RegionChange 3
////tostring
template <class TYPE> std::string tostring(const TYPE & t ) {
std::ostringstream os;
os << t;
return os.str();
};
#endif //__GLOBAL__
只能在初始化列表中初始化当前类的成员。Name
(和Cost
)都是基类的成员;它们必须在基类构造函数中初始化。
最简单的方法是向Shopable
添加构造函数:
class Shopable {
...
public:
Shopable(std::string n, int c, std::string d)
: Name(n), Cost(c), Description(d) {}
...
};
,然后在Weapon
初始化列表中使用:
class Weapon : public Shopable {
...
public:
Weapon(int c,int d,std::string n)
: Shopable(n,c,""), Damage(d)
{}
};
In
Weapon(int c,int d,std::string n) : Cost(c), Damage(d), Name(n){}
不能在初始化列表中使用基类的成员。相反,在Shoppable
中定义一个合适的构造函数,并像这样调用它:
Weapon(int c,int d,std::string n) : Shoppable(c, d, n)
应该为Shopable创建一个构造函数,并使用该构造函数初始化继承的成员。
class Shopable{
protected:
std::string Name;
int Cost;
std::string Description;
public:
std::string getName() const{return Name;}
int getCost() const {return Cost;}
virtual std::string getDesc() const = 0;
public:
Shopable(std::string n, int c, std::string d) : Name(n), Cost(c), Description(d) {}
};
和
class Weapon : public Shopable{
private:
int Damage;
public:
Weapon(int c,int d,std::string n) : Shopable(n, c, "Weapon"), Damage(d) {} // should probably reorder the parameters to match, just for consistency
std::string getDesc() const{
return getName()+"t"+tostring(Damage)+"t"+tostring(Cost);
}
int getDamage() const{return Damage;}
int DamageTarget(Entity* target){
int DamageDealt = 0;
//do damage algorithm things here
return DamageDealt;
}
};
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- C++如何用类和函数将我的代码翻译成OOP
- 将OOP与结构代码混合
- 为什么这个代码不能工作?(c++ OOP)公共继承
- 大量的错误与杂乱的OOP代码有关