Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 与混乱的OOP代码相关的大量错误_C++_Oop - Fatal编程技术网

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

C++ 与混乱的OOP代码相关的大量错误,c++,oop,C++,Oop,当我编译的时候,我得到了大量的错误。如果我解决了其中一个问题,希望事情对我来说会变得更清楚,否则我可以发布其余的: “武器”:非法成员初始化:“名称”不是基或成员 它的名字和成本都是这样的。武器继承可购物品,可购物品的名称、成本和描述在其受保护部分 可购买。h: #ifndef _SHOPABLE_H_ #define _SHOPABLE_H_ #include "Library.h" class Shopable{ protected: std::string Name;

当我编译的时候,我得到了大量的错误。如果我解决了其中一个问题,希望事情对我来说会变得更清楚,否则我可以发布其余的:

“武器”:非法成员初始化:“名称”不是基或成员

它的名字和成本都是这样的。武器继承可购物品,可购物品的名称、成本和描述在其受保护部分

可购买。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
武器.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 Weapon : public Shopable {
    ...
public:
    Weapon(int c,int d,std::string n)
    : Shopable(n,c,""), Damage(d)
    {}
};

不能在初始化列表中使用基类的成员。相反,在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) {}
};


我如何创建一个初始化所有3个的构造函数?@pighead Weaponint c,int d,std::string n{Cost=c;Damage=d;Name=n;}willdo@J.S.泰勒:那是可行的,但有一个更好、更普遍的解决方案。
Weapon(int c,int d,std::string n) : Shoppable(c, d, n)
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;
    }
};