类为参数错误 [英] Classes as parameters error

查看:110
本文介绍了类为参数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Weapon.h, when I try and take a class 'Entity*' as a parameter, it says "Syntax error: identifier 'Entity'" when I compile. Additionally when I roll over the text 'target', Visual C++ Express 2010 gives me the text " *target". The Entity class is fine and I'm pretty sure it's included correctly.

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

(I won't post Player.h as it's unnecessary - see Library.h - but it has a header guard and includes Entity.h)

Library.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__

武器.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

Shopable.h:

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

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

推荐答案

您的标头彼此包含,因为您的类相互引用. (但是您的编译器不会因为包含include防护而遭受stackoverflow的困扰,这是一件好事!)

Your headers include each other because your classes refer to each other. (But your compiler doesn't suffer from a stackoverflow because of your include guards - that's a good thing!)

您应该按层次排列头文件,即,在顶部"的文件中不包含任何内容,而在下方"的文件中包含一些顶部的文件,等等.但是绝不应该有循环".

You should arrange your header files hierarchically, ie there are files at the 'top' which #include nothing and files 'below' which include some of the top ones and so-on down the hierarchy. But at no point should there be 'loops'.

为了打破代码中的循环,任何互相引用的类都应转发声明任何相互依赖项,并且仅引用依赖项名称,而不是它们的成员.

In order to break your loops in your code, any classes that refer to each other should forward declare any mutual dependencies and only refer to dependency names and not their members.

例如

Entity.h

class Weapon;
class Entity{
    ...
    Weapon* weapon;
};

武器.h

class Entity;
class Weapon{
    ...
    int DamageTarget(Entity* target);
};

请注意Weapon.h仅指Entity*.

Notice how Weapon.h only refers to Entity*.

您需要在Weapon.cpp中定义int Weapon::DamageTarget(Entity* target)

You will need to define int Weapon::DamageTarget(Entity* target) in Weapon.cpp

这篇关于类为参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆