我正在尝试创建一个定义项目的类和另一个定义库存的类,其中包含项目的向量列表。但是,通过下面的解决方案,我遇到了多个错误,最值得注意的是
'std::vector': no appropriate default constructor available
...以及其他我只能假设从中得出的结论。这是我的定义:
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;
}
库存.h
#include "header.h"
class Inventory
{
private:
std::vector<Item> itemlist;
public:
Inventory();
std::string getInventory();
void addItem(Item x);
};
库存.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);
}
我有一种感觉,这与我的自定义对象在某种程度上与我尝试使用向量的方式不兼容有关。这一切是否存在根本性的错误,或者我只是在某个地方犯了一个简单的错误?
添加默认构造函数没有任何改变,但是添加: itemlist(0)
Inventory 构造函数的初始化程序删除了该特定错误。然而,这两个错误的多个实例仍然发生:
'Item': undeclared identifier
and
'std::vector': 'Item' is not a valid template type argument for parameter '_Ty'
我想知道我的两个单独的类是否存在某种范围问题?