简体   繁体   English

使用带向量的自定义类:'std :: vector'默认构造函数错误

[英]Using custom class with vectors: 'std::vector' default constructor error

EDIT: Adding in a default constructor changed nothing, but adding in a : itemlist(0) initialiser to the Inventory constructor removed that particular error. 编辑:添加默认构造函数没有任何改变,但在库构造函数中添加: itemlist(0)初始化程序删除了该特定错误。 However, multiple instances of these two error still occur: 但是,仍会出现这两个错误的多个实例:

'Item': undeclared identifier

and

'std::vector': 'Item' is not a valid template type argument for parameter '_Ty'

I'm wondering if there's some sort of scope issue happening here as regards to my two separate classes? 我想知道在这两个不同的课程中是否存在某种范围问题?


I'm trying to create one class which defines an Item and another class which defines an Inventory, containing a vector list of Items. 我正在尝试创建一个定义Item的类和另一个定义Inventory的类,其中包含Items的向量列表。 However, with the solution below, I'm getting multiple errors, most notably 但是,通过下面的解决方案,我得到了多个错误,最值得注意的是

'std::vector': no appropriate default constructor available

...and others which I can only assume lead on from that. ...以及其他我只能从​​中承担的领导。 Here's my definitions: 这是我的定义:

header.h header.h

#include <iostream>
#include <string>
#include <vector>
#include "Item.h"
#include "Inventory.h"

Item.h Item.h

#include "header.h"
class Item
{
private:
    std::string name;
public:
    Item(std::string n, std::string d, int c);
    std::string getName();
};

Item.cpp Item.cpp

#include "header.h"
using namespace std;

Item::Item(string n)
{
    name = n;
}

string Item::getName()
{
    return name;
}

Inventory.h Inventory.h

#include "header.h"

class Inventory
{
private:
    std::vector<Item> itemlist;

public:
    Inventory();
    std::string getInventory();
    void addItem(Item x);
};

Inventory.cpp Inventory.cpp

#include "header.h"
using namespace std;

Inventory::Inventory()
{
}

string Inventory::getInventory()
{
    string output = "";

    for (int i = 0; i <= itemlist.size(); i++)
    {
        output = output.append(itemlist[i].getName());
    }

    return output;
}

void Inventory::addItem(Item x)
{
    itemlist.push_back(x);
}

I have a feeling it's something to do with my custom-defined object being somehow incompatible with vectors in the way I've attempted to use them. 我有一种感觉,这与我的自定义对象在某种程度上与我试图使用它们的方式与向量不兼容有关。 Is there something fundamentally wrong with all of this or have I just made a simple mistake somewhere? 所有这些都存在根本性的错误,或者我在某个地方犯了一个简单的错误?

You need to have a default constructor to use std::vector. 你需要一个默认的构造函数来使用std :: vector。 The default constructor is the one that has no arguments, ie, Item::Item() { ... } 默认构造函数是没有参数的构造函数,即Item::Item() { ... }

As mentioned in std::vector<> s reference documentation (emphasis mine): std::vector<> s 参考文档中所述 (强调我的):

T The type of the elements. T元素的类型。
must meet the requirements of CopyAssignable and CopyConstructible . 必须满足CopyAssignable和CopyConstructible的要求。
The requirements that are imposed on the elements depend on the actual operations performed on the container. 对元素施加的要求取决于对容器执行的实际操作。 Generally, it is required that element type is a complete type and meets the requirements of Erasable, but many member functions impose stricter requirements. 通常,要求元素类型是完整类型并满足Erasable的要求,但许多成员函数强加了更严格的要求。

So you still need to provide a copy constructor and assignment operator as well. 所以你仍然需要提供一个拷贝构造函数和赋值运算符。 Also Item needs to be completely declared, when vector<Item> is instantiated. Item需要完全声明,当vector<Item>被实例化。


You can store a smart pointer in your vector though, if it's not possible to provide the required functions for your class, eg: 如果无法为您的类提供所需的功能,您可以在vector存储智能指针,例如:

std::vector<std::unique_ptr<Item>> itemlist;

or 要么

std::vector<std::shared_ptr<Item>> itemlist;

This would have the advantage that you don't have your Item instances copied all the time. 这样做的好处是您不会一直复制Item实例。

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

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