作为参数的类错误

pig*_*d10 0 c++ oop parameters class function

在Weapon.h中,当我尝试将类'Entity*'作为参数时,它在编译时会显示"语法错误:标识符'实体'".另外,当我滚动文本'target'时,Visual C++ Express 2010为我提供了文本"*target".Entity类很好,我很确定它包含正确.

(我不会发布Player.h,因为它是不必要的 - 请参阅Library.h - 但它有一个标题保护并包含Entity.h)

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__
Run Code Online (Sandbox Code Playgroud)

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) : Shopable(c,n),Damage(d){}
    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
Run Code Online (Sandbox Code Playgroud)

Shopable.h:

#ifndef _SHOPABLE_H_
#define _SHOPABLE_H_

#include "Library.h"

class Shopable{
protected:
    std::string Name;
    int Cost;
    std::string Description;
public:
    Shopable(int c, std::string n):Cost(c),Name(n){}
    std::string getName() const{return Name;}
    int getCost() const {return Cost;}
    virtual std::string getDesc() const = 0;
};

#endif
Run Code Online (Sandbox Code Playgroud)

Entity.h:

#ifndef _ENTITY_
#define _ENTITY_

#include "Library.h"
#include "Weapon.h"
#include "Armour.h"
#include "Consumable.h"

class Entity{
public:
    void printStats() const;
    void heal(double health);
    std::string name;
protected:
    //player stats
    double str;     //strength
    double wis;     //wisdom
    double ref;     //reflex
    double hp;      //health points
    double maxHp;   //maximum health points
    double i;       //initiative
    double inte;    //intelligence
    double c;       //courage
    int gold;       //gold
    int xp;         //experience
    int ap;         //armour points
    int wd;         //weapon damage
    int lvl;        //level
    int sp;         //skill points
    Weapon* weapon;//weapon
    Armour* cArmour;//current armour
};

#endif
Run Code Online (Sandbox Code Playgroud)

Oli*_*rth 5

在C++中,必须在引用类之前声明它们.你在#includeEntity.h中是-ing Weapon.h,但是那时,编译器不知道是否存在class Entity.

您将需要更改声明事物的顺序,或者在"上方" 添加前向声明class Weapon.它可以简单地是:

class Entity;
Run Code Online (Sandbox Code Playgroud)

这告诉编译器有这样的名字Entity.然而,它并没有告诉它什么关于它所含的成员,所以你不能真正用它做任何事情,除了声明变量的Entity *Entity &,并通过他们周围.