【发布时间】:2017-03-04 12:11:30
【问题描述】:
我的结构数组的文件写入功能出现问题。我遇到了错误,could not convert 'cars[n]' from 'car' to 'std::string {aka std::basic_string<char>}'
我对文件写入有点困惑,也许有人可以解释或给我一些提示如何使我的写入功能正常工作?
我的代码:
#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <fstream>
using namespace std;
#define N_CARS 2
struct car{
string model;
int year;
double price;
bool available;
}cars [N_CARS];
void writeToFile(ofstream &outputFile, string x )
{
outputFile << x << endl;
}
int main ()
{
string mystr;
string mystr2;
string mystr3;
int n;
for (n=0; n<N_CARS; n++)
{
cout << "Enter title: ";
getline (cin,cars[n].model);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> cars[n].year;
cout << "Enter price: ";
getline (cin,mystr2);
stringstream(mystr2) >> cars[n].price;
cout << "Choose availability: ";
getline (cin,mystr3);
stringstream(mystr3) >> cars[n].available;
}
ofstream outputFile;
outputFile.open("bla.txt");
for (n=0; n<N_CARS; n++)
writeToFile(outputFile, cars[n]);
outputFile.close();
system("PAUSE");
return 0;
}
outputFile << x << endl; 将写入文件来归档我的整个结构字段是否正确?
【问题讨论】:
-
确定错误信息与此错误无关:
writeToFile(outputFile, cars[n]);? -
编译器显示问题存在。但是我该如何改变呢?
-
再次检查函数的参数类型。
Car可以转换成string吗?
标签: c++ struct writetofile