2014-04-28 96 views
1

我有这两个类需要对方,并从同一类继承。我编译了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 

Buyer.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 
+4

您有循环包含依赖项。这是行不通的。看到http://stackoverflow.com/questions/625799/resolve-circular-dependencies-in-c – juanchopanza

+0

我现在看到它。但是这个Order类怎么样?这不是真的循环,我也得到一个错误 – user3582405

回答

1

你有SellerBuyer之间的循环依赖。这将无法正常工作,因为编译器需要声明Seller才能编译Buyer ...但它也需要声明Buyer来编译Seller

您可以改为预先声明您的类,因为您实际使用的都是指向这些类型的指针。例如:

#ifndef SELLER_H 
#define SELLER_H 
#include "Entity.h" 
#include <string> 
#include <vector> 
#include "Inventory.h" 
#include "Order.h" 

// forward declaration of Buyer 
class Buyer; 

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 

如果你有中Buyer实例为Seller成员(即Buyer _buyer;),或任何方法了/返回的Buyer一个实例,你将不得不改变你的结构。既然你没有这个问题,那么一个前向声明就足够了。


顺便说一句,并承认,我并不了解你的程序的结构,它通常是一个不好的征兆当人们看到在C++程序正在使用这么多的裸指针。您可以存储实例。您可以使用安全指针(shared_ptrunique_ptr),具体取决于您的所有权语义。

例如,addBuyer可以轻松取Buyer&而不是指针。现在你不必担心无效指针。我假设你将这些添加到你的buyers矢量中......但你如何保证这些指针在Seller实例的生命周期中保持有效?你不能;你受任何人称为addBuyer的摆布。

为什么不只是存储std::vector<Buyer>并在您的添加方法中引用?拷贝的成本太高以至于不能保证这种设计?如果是这样,你不能使用shared_ptr

+0

这是一个项目,我给了一些指示,如使用指针等。 – user3582405

+0

我有一个关于Order类的问题。由于我的卖家阶层也给我一个错误。为什么它超出了范围?我将其包含在内 – user3582405

+0

@ user3582405:那么,你必须发布你的'订单'声明,但它可能是同样的问题? 'Order'是否使用'Seller'? –