我有此头文件,并且正在尝试创建type变量Item。我已经包含了#include "Item.h",但是unknown type name Item在编译时两个私有变量仍然出现错误。
#ifndef PLAYER_H
#define PLAYER_H
#include <vector>
#include "Item.h"
using std::vector;
class Player
{
public:
// constructor
Player( void );
// destructor
virtual ~Player( void );
private:
Item item;
std::vector <Item> inventory;
};
#endif /* PLAYER_H */
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?
Item.h我包括的继承人
#ifndef ITEM_H
#define ITEM_H
#include <string>
#include "Player.h"
#include "GlobalDefs.h"
class Item {
public:
Item();
Item(gold_t v, std::string n);
virtual ~Item();
// Getter
inline virtual gold_t GetValue (void)
{
return value;
}
// Getter
inline virtual std::string GetName (void);
// Getter
virtual std::string GetItemText(void);
protected:
gold_t value;
std::string name;
};
#endif /* ITEM_H */
Run Code Online (Sandbox Code Playgroud)
如果您Item.h从cpp文件Player.h中包含,则从文件中包含。然后,再次Player.h包含Item.h,但多亏了包含保护,这几乎没有任何作用。
然后,在included 中Player.h,还没有Item声明no 。因此,编译器会发出错误。
由于 中没有使用 from ,因此从Player.h中Item.h删除。#include "Player.h"Item.h
| 归档时间: |
|
| 查看次数: |
13097 次 |
| 最近记录: |