现在我有一个名为void
的{{1}}函数。我想将此函数中的所有内容显示为文本文件。以下是我的代码,但它不起作用。
displayAllCars(Cars)
错误是"不匹配运营商<<在salesAnalysis"
答案 0 :(得分:1)
ostream & displayAllCars(ostream & ostr, vector<Vehicle*>& cars)
{
for(int i = 0; i < cars.size(); i++)
{
ostr << i+1 <<". ";
cars[i]->display(ostr);
ostr << endl;
}
}
ofstream salesAnalysis("salesAnalysis.txt");
displayAllCars(salesAnalysis, cars);
salesAnalysis << "Total Sales for the Company(RM): " << totalSales << endl
答案 1 :(得分:1)
您需要重载运算符&lt;&lt; :
ostream& operator << (ostream &os, const vehicle &vc) {
vc.display(os);
return os;
}
ostream& operator << (ostream &os, const vector<vehicle*> &vc) {
for(int i = 0; i < vc.size; ++i) {
os << *vc[i] << " "; // after *vc[i] some separator or std::endl
}
return os;
}