【问题标题】:Implicit instantiation of undefined template 'std::__1::vector<Item *>' error未定义模板 'std::__1::vector<Item *>' 错误的隐式实例化
【发布时间】:2021-09-02 00:15:44
【问题描述】:

我正在尝试将类的指针存储在向量中,但出现错误

未定义模板'std::__1::vector'的隐式实例化

代码是:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Item{
private:
    string itemName;
    float itemPrice;
    int itemQuantity;
public:
    Item(string name = "NULL", float price = 0, int quantity = 0){
        
        itemName = name;
        itemPrice = price;
        itemQuantity = quantity;
    
        
    }
    friend ofstream & operator<<(ofstream &ofs, Item &i);
    friend ifstream & operator>>(ifstream &ifs, Item &i);
    friend ostream & operator<<(ostream &os, Item &i);
    

};

ofstream & operator<<(ofstream & ofs, Item &i){
    
    ofs << i.itemName << endl;
    ofs << i.itemPrice << endl;
    ofs << i.itemQuantity << endl;
    return ofs;
    
    
    
    
}

ostream & operator<<(ostream &os, Item &i){

    os << i.itemName << endl;
    os << i.itemPrice << endl;
    os << i.itemQuantity << endl;
    
    return os;
}

ifstream & operator>>(ifstream & ifs , Item & i){
    ifs >> i.itemName;
    ifs >> i.itemPrice;
    ifs >> i.itemQuantity;
    
    return ifs;
    
    
}
int main() {
int n;
string name;
float price;
int qty;
cout<<"Enter number of Item:";
cin>>n;
vector<Item *> items;
cout<<"Enter All Item "<<endl;
for(int i=0;i<n;i++)
{
    cout<<"Enter "<<i+1<<" Item Name , price and quantity";
    cin>>name;
    cin>>price;
    cin>>qty;
    items.push_back(new Item(name,price,qty));
}
ofstream fos("Items.txt");
for(int i=0;i<n;i++)
{
    fos<<*items[i];
}
Item item;
ifstream fis("Items.txt");
for(int i=0;i<3;i++)
{
    fis>>item;
    cout<<"Item "<<i<<item<<endl;
}
}

这看起来像是一个基本错误,但我找不到解决方法。如何将用户定义类的指针存储在向量中?是我的语法关闭还是向量不能那样工作?

【问题讨论】:

  • 你不见了#include &lt;vector&gt;
  • 在明显的#include &lt;vector&gt; 之后,它为我编译并运行。

标签: c++ pointers vector


【解决方案1】:

你没有加入

#include <vector>

添加它,您的代码将编译干净。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多