简体   繁体   English

C ++如何将对象向量声明为类的成员

[英]C++ how to declare a vector of objects as a member of a class

I'm trying to declare a vector<Item> as a private member of another class Inventory , but it is giving me an error saying that Item is not in scope. 我试图将vector<Item>声明为另一个类Inventory的私有成员,但这给我一个错误,指出Item不在范围内。 Both classes are declared in the same file. 这两个类都在同一文件中声明。 I don't know how to change the scope that it looks at or whatever you are supposed to do to make it work. 我不知道如何更改其外观的范围,或者不知道如何使它起作用。

Here is the code to make absolutely clear what I'm trying to do. 这是完全清楚我要做什么的代码。

class Inventory {
public:

private:
    vector<Item> inventory;
};

class Item {
public:
    void SetName(string nm)
        { name = nm; };
    void SetQuantity(int qnty)
        { quantity = qnty; };
    void SetPrice(int pric)
        { price = pric; };
    virtual void Print()
        { cout << name << " " << quantity << " for $" << price 
          << endl; };
    virtual ~Item()
        { return; };
protected:
    string name;
    int quantity;
    int price;
};

Item must be defined before its usage as a template argument. 必须在将Item用作模板参数之前对其进行定义。

Technically, you may be able to get away with a forward declaration in specific contexts, but to save you time and frustration with learning the exact rules, it is easier to just make sure you have defined it first. 从技术上讲,您也许可以在特定情况下放弃前向声明,但是为了节省时间和学习确切的规则而感到沮丧,仅需先定义就容易了。

In general, the order of declarations are important. 通常,声明的顺序很重要。 If you use a type in the declaration of another type, the used type must already be defined. 如果在其他类型的声明中使用一个类型,则必须已经定义了使用的类型。 Exceptions to this rule involve usage by pointer and reference which only require forward declaration. 该规则的例外情况涉及指针和引用的使用,它们仅需要向前声明。

As std::vector<Item> is a type in its own right, it must be declared after the declaration of the Item class. 由于std::vector<Item>本身就是一种类型,因此必须在Item类的声明之后进行声明

(It's similar to the rule that for class Child : public Base , the declaration of Base needs to appear above that line). (这是类似的规则class Child : public Base ,申报Base需要出现上面那条线)。

A forward declaration is not insufficient. 向前声明 并不足够。

One way round this is to use a std::vector<std::shared<Item>> (a vector of smart pointers) but that, of course, changes the structure of the vector. 解决这个问题的一种方法是使用std::vector<std::shared<Item>> (智能指针的向量),但是当然会改变向量的结构。 In that case a forward declaration is sufficient. 在那种情况下,向前声明足够了。

Define Item first, then Inventory. 首先定义物料,然后定义库存。

class Item {

public:
    void SetName(string nm)
        { name = nm; };
    void SetQuantity(int qnty)
        { quantity = qnty; };
    void SetPrice(int pric)
        { price = pric; };
    virtual void Print()
        { cout << name << " " << quantity << " for $" << price 
          << endl; };
    virtual ~Item()
        { return; };
protected:
    string name;
    int quantity;
    int price;
};

class Inventory {
public:

private:
    vector<Item> inventory;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM