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

…衆ロ難τιáo~ 提交于 2021-01-29 05:52:03

问题


EDIT: Adding in a default constructor changed nothing, but adding in a : itemlist(0) initialiser to the Inventory constructor removed that particular error. 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. 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

#include <iostream>
#include <string>
#include <vector>
#include "Item.h"
#include "Inventory.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

#include "header.h"
using namespace std;

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

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

Inventory.h

#include "header.h"

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

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

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?


回答1:


You need to have a default constructor to use std::vector. The default constructor is the one that has no arguments, i.e., Item::Item() { ... }




回答2:


As mentioned in std::vector<>s reference documentation (emphasis mine):

T The type of the elements.
must meet the requirements of CopyAssignable and 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.

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.


You can store a smart pointer in your vector though, if it's not possible to provide the required functions for your class, e.g.:

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.



来源:https://stackoverflow.com/questions/33109791/using-custom-class-with-vectors-stdvector-default-constructor-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!