c++ - 类(class)尚未宣布但已包括在内?

标签 c++ compiler-errors scope

<分区>

我有这两个类,它们相互需要并且继承自同一个类。我编译 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

最佳答案

你在 Seller 之间有一个循环依赖和 Buyer .这永远不会起作用,因为编译器需要声明 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 vector ...但是你如何保证这些指针在 Seller 的生命周期内保持有效?实例?你不能;你任凭谁叫addBuyer .

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

关于c++ - 类(class)尚未宣布但已包括在内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23347826/

相关文章:

c++ - 如何在 C++ 应用程序中重现 "Stack smashing detected"

c++ - 使用 openssl 和 c++ 生成 OAuth 访问 token

compiler-errors - Webstorm 7的TypeScript转码错误

javascript - 变量范围

javascript - 为什么这个输出未定义?

c++ - 是否可以使用 operator new 和 initialiser 语法初始化非 POD 数组?

c++ - 在同一个类/结构中具有相同签名的相同函数?重载?

c++ - 带有#include的C++未知重写说明符

ios - 字符串插值解析无法正常工作

javascript - 为什么 javascript "for loop"对不同类型的对象有不同的行为