<分区>
我有这两个类,它们相互需要并且继承自同一个类。我编译 Seller.h,它编译正常,但是当我编译 Buyer.h 时,我从 Seller.h 中得到错误。
因此,当我编译 Buyer.h 时,我得到如下错误:
Seller.h:14:16: error: âBuyerâ has not been declared
void addBuyer(Buyer*);
^
Seller.h:15:14: error: âBuyerâ was not declared in this scope
std::vector<Buyer*> getBuyers() const;
Seller.h:20:17: error: âOrderâ has not been declared
void fillOrder(Order*);
^
它们是#included,但它仍然说超出范围。
#ifndef SELLER_H
#define SELLER_H
#include "Entity.h"
#include <string>
#include <vector>
#include "Inventory.h"
#include "Buyer.h"
#include "Order.h"
class Seller : public virtual Entity
{
public:
Seller(const std::string &, const std::string &, double=0.0);
virtual~Seller(){}
void addBuyer(Buyer*);
std::vector<Buyer*> getBuyers() const;
void setInventory(Inventory*);
Inventory* getInventory() const;
virtual void list() const override;
virtual void step() override;
void fillOrder(Order*);
private:
Inventory* inv;
std::vector <Buyer*> buyers;
};
#endif
买家.h
#ifndef BUYER_H
#define BUYER_H
#include <string>
#include "Entity.h"
#include <queue>
#include "Order.h"
#include "Seller.h"
class Buyer : public virtual Entity
{
public:
Buyer(const std::string &, const std::string &, double =0.0 );
virtual ~Buyer(){}
void addSeller(Seller *);
std::queue <Seller *> getSellers() const;
void addOrder(Order *);
std::queue <Order*> getOrders() const;
virtual void list() const override;
virtual void step() override;
private:
std::queue <Order*> orders;
std::queue <Seller*> sellers;
};
#endif