【发布时间】:2020-08-29 05:54:11
【问题描述】:
这里我有一个叫做contacts的结构
typedef struct contacts
{
string name; //{jhonathan , anderson , felicia}
string nickName; //{jhonny , andy , felic}
string phoneNumber; // {13453514 ,148039 , 328490}
string carrier; // {atandt , coolmobiles , atandt }
string address; // {1bcd , gfhs ,jhtd }
} contactDetails;
vector <contactDetails> proContactFile;
我正在尝试将向量中的数据写入输出文件。为此,我编写了以下代码
ofstream output_file("temp.csv");
int selectContact;
cout << "Which Contact you want to delete ? Enter the relevent index " << endl;
cin >> selectContact;
if (selectContact > proContactFile.size()) {
cout << "Invalid entry";
}
else {
proContactFile.erase(proContactFile.begin() + (selectContact - 1));
cout << "Delete successfully";
}
ostream_iterator<contactDetails> output_iterator(output_file, "\n");
copy(begin(proContactFile),end(proContactFile), output_iterator);
}
output_file.close();
fin.close();
remove("Contact.csv");//Deletes contacts.csv file
rename("temp.csv" , "Contact.csv");
但是这段代码总是给我一个错误。另外我想用以下方式将数据写入文件。
Name,Nick name,Phone number,Carrier,Address
我的代码有什么问题?(基本上我在这里做的是让用户删除向量中的一个元素并将更新的向量写入输出文件)
这是我收到的错误:Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'const _Ty' (or there is no acceptable conversion)
【问题讨论】:
-
这里的意思是你已经成功地将你的数据写入到你代码的另一部分的文件中。那么这段代码和好代码有什么区别,或者为什么这里不能用好代码代替上面的呢?
-
不要让我们猜测,说出错误是什么。我有一个想法,但您最好确认一下。
-
你遇到了什么错误?
-
ofstream output_file已被声明了两次...这是错字吗? -
@john 我真的很抱歉。我得到的错误是
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'const _Ty' (or there is no acceptable conversion)