一个c++ stl相关的问题 ,可以说很简单,可以说很难
这个题目是卡耐基 梅农大学的ssd 课程的一个题目
在main.cpp里面
有三个函数是的申明已经给出 ,代码实现要自己写
这个三个函数是 find_car , handle_arrival, handle_departure
下面是 main.cpp的 代码
代码有点长,真的不好意思,但是为了把问题说清楚,我只能把全部代码贴出来
#pragma warning(disable:4786)
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <stack>
#include <stdexcept>
#include <vector>
#include <algorithm>
#include "car.h"
using namespace std;
const int PARKING_SPOTS_PER_AISLE = 3;
const int NUMBER_OF_AISLES = 5;
void handle_arrival(vector<Car>&, vector<stack<string> >&, const string&);
void handle_departure(vector<Car>&, vector<stack<string> >&, const string&);
Car& find_car(vector<Car>&, string);
int main(int argc, char* argv[]) {
try {
if (argc != 2) {
cerr << "Usage:\n" << argv[0] << " data-file";
return EXIT_FAILURE;
}
ifstream inf(argv[1]);
if (! inf) {
cerr << "Could not open " << argv[1];
return EXIT_FAILURE;
}
vector<Car> cars;
vector< stack<string> > parking_lot(NUMBER_OF_AISLES);
while (! inf.eof()) {
string action, plate;
inf >> plate >> action;
if (action == "arrives") {
handle_arrival(cars, parking_lot, plate);
}
else if (action == "departs") {
handle_departure(cars, parking_lot, plate);
} else {
cerr << "Unknown action: " << action << endl;
}
}
inf.close();
cout << "\nHere are all the cars that visited the lot today:\n";
ostream_iterator<string> output(cout,"");
sort(cars.begin(),cars.end(),cars);
copy(cars.begin(),cars.end(),output);
return EXIT_SUCCESS;
}
catch (exception& e) {
cerr << e.what() << endl;
}
catch (...) {
cerr << "Unknown exception caught!" << endl;
}
return EXIT_FAILURE;
}
void handle_arrival(vector<Car>& cars, vector< stack<string> >& parking_lot, const string& plate) {
for(int i=0;i<parking_lot.size();i++){
stack<string> s=parking_lot[i];
if(s.size()!=3){
parking_lot[i].push(plate);
Car c(plate,i+1);
cars.push_back(c);
cout<<plate<<"park";
return ;
}
}
cerr<<" all the parking_lot is full";
}
void handle_departure(vector<Car>& cars, vector< stack<string> >& parking_lot, const string& plate) {
Car departCar=find_car(cars,plate);
int i=departCar.getAisle();
stack<string> temp;
stack<string> leave =parking_lot[i-1];
string license=leave.top();
leave.pop();
while(license!=plate){
temp.push(license);
license=leave.top();
leave.pop();
}
while(temp.size()>0){
string l=temp.top();
temp.pop();
leave.push(l);
}
cout<<plate<<" departure";
// cout<<
}
Car &find_car(vector<Car>& cars, string plate) {
Car n(plate);
Car *p=&n;
Car *r;
*r=find(cars.begin(),cars.end(),p);
return *r;
}
另外car.cpp和car.h 是题目给的代码,不能改动的。
现在编译出现这个错误
C:\Documents and Settings\pineapple\桌面\main.cpp(118) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class Car *' (or there is no acceptable conversion)
而且在find-car 函数里面 ,各种各样的方法我都试验过
下面回帖的人 如果不能确定是100% 正确的,不要随便说
因为这个问题 我已经搞了 n天鸟
希望 是能够可以运行的程序
下面把car.h 和car.cpp也贴出来