与混乱的OOP代码相关的大量错误

如何解决与混乱的OOP代码相关的大量错误

| 编译时出现了很多错误。希望如果我解决其中之一,事情对我来说会变得更加清晰(如果没有,我可以将其余的发布):
\'Weapon\' : illegal member initialization: \'Name\' is not a base or member
它具有名称和成本。武器继承Shopable,并且Shopable在其受保护的部分具有名称,成本和说明。 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__
Globals.h:
//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) {}
    ...
};
然后在“ 8”初始化列表中使用它:
class Weapon : public Shopable {
    ...
public:
    Weapon(int c,int d,std::string n)
    : Shopable(n,c,\"\"),Damage(d)
    {}
};
    ,在
Weapon(int c,std::string n) : Cost(c),Damage(d),Name(n){}
您不能在初始化列表中使用基类的成员。而是在
Shoppable
中定义一个适当的构造函数,并按如下方式调用它:
Weapon(int c,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,std::string d) : Name(n),Description(d) {}
};
class Weapon : public Shopable{
private:
    int Damage;
public:
    Weapon(int c,std::string n) : Shopable(n,\"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;
    }
};
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?