【发布时间】:2022-01-21 17:39:12
【问题描述】:
我正在尝试为一个名为customer 的结构创建一个结构向量——稍后我将在其他函数中使用它。但是,当我使用push_back() 函数时,出现以下错误
在模板中:调用隐式删除的“客户”复制构造函数
谁能解释一下为什么我会收到这个错误?
这是与我的问题相关的部分代码:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct cartitem{
//members of the struct
};
struct customer{
string name;
string arrival_time;
float total_price = 0.0;
vector<cartitem> cart;
fstream mycart;
};
vector<customer> Customers;
static customer newCustomer(string cname, string arrtime){
customer b;
b.name = cname;
b.arrival_time = arrtime;
Customers.push_back(b);
return b;
};
【问题讨论】:
-
Customers 不是静态的,但 newCustomer 是。这可能不是你想要的。
-
真正的罪魁祸首是
fstream成员。流不可复制。 -
您希望
newCustomer创建一个新客户,将其副本添加到Customers,然后返回您刚刚创建的客户吗?无论如何,fstream是不可复制的。目前还不清楚你在用它做什么,所以很难说应该对customer和/或newCustomer进行哪些更改