一个c++ stl相关的问题 ,可以说很简单,可以说很难

molinus 2006-05-15 12:35:01
这个题目是卡耐基 梅农大学的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也贴出来
...全文
385 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jesse8013 2006-05-25
  • 打赏
  • 举报
回复
你前面的程序有问题
至于那个find_car
Car &find_car(vector<Car>& cars, string plate) {
Car n(plate);
Car *r;
*r=find(cars.begin(),cars.end(),p);
return *r;
}
就可以前提是你前面的要正确.
如果有问题 ,继续和我联系
molinus 2006-05-25
  • 打赏
  • 举报
回复
楼上的方法换汤不换药
molinus 2006-05-15
  • 打赏
  • 举报
回复
下面是题目的说明
其实贴出拉也没什么用Tasks
To complete this assessment, you will need to finish the implementation of the parking-lot simulation.

To begin, verify the files needed for this assessment.

Extract the archive to retrieve the files needed to complete this assessment.
Following is an ordered list of steps that serves as a guide to completing this assessment. Work and test incrementally. Save often

First, finish the implementation of function find_car. This function returns a reference to the Car object stored in the vector cars whose license plate equals the parameter plate. Use the STL find function to perform this task. To use this function correctly, you must supply it with three arguments. The first two arguments specify a range to search. The third argument is the value that the function attempts to find. This argument must be of type Car.
Next, finish the implementation of function handle_arrival. This function should iterate through the vector of stacks, looking for the first stack that does not contain three cars. If all five aisles (stacks) are full, output a message indicating such; otherwise place the license plate into the first non-full stack. This is essentially "parking" the car. For this arriving car, also add an entry of type Car to the vector cars. In this Car instance, make sure to record properly the index of the aisle where this car is parked.
Then, finish the implementation of function handle_departure. This function should locate the departing vehicle from the cars vector using function find_car. Then this function should remove the departing car's license plate from the appropriate aisle. Another stack must be used to move, temporarily, any cars that may be in front of the departing car. Record the number of times a car is moved when accommodating the departure of another car. For the departing car, display the number of times it was moved while it was parked in the lot.
molinus 2006-05-15
  • 打赏
  • 举报
回复
car.h
#include <iostream>
#include <string>
#ifndef _CAR_H_
#define _CAR_H_

using namespace std;

class Car {

private:
int moved;
string license_plate;
int aisle;

public:
Car(string);
Car(string, int);

int getTimesMoved(void) const;
void setTimesMoved(int);
string getPlate(void) const;
int getAisle(void) const;

bool operator < (const Car& rhs) const;
bool operator == (const Car& rhs) const;
};

#endif

car.cpp

#include "car.h"

Car::Car(string plate) :
license_plate(plate),
moved(0), aisle(-1) {}

Car::Car(string plate, int aisle) :
license_plate(plate),
moved(0), aisle(aisle) {}

int Car::getTimesMoved(void) const {

return moved;
}

void Car::setTimesMoved(int m) {

moved = m;
}

int Car::getAisle(void) const {

return aisle;
}

string Car::getPlate(void) const {

return license_plate;
}

bool Car::operator < (const Car& rhs) const {

return this->getPlate() < rhs.getPlate();
}

bool Car::operator == (const Car& rhs) const {

return this->getPlate() == rhs.getPlate();
}
roger_77 2006-05-15
  • 打赏
  • 举报
回复
偶看完了。

不知能否

Car &find_car(vector<Car>& cars, string plate) {
Car n(plate);
// Car *p=&n;
// Car *r;
vector<Car>::iterator itr=NULL;
itr = find(cars.begin(),cars.end(),n);

return *itr;
}
delphihero 2006-05-15
  • 打赏
  • 举报
回复
Car *r;
*r=find() Car 类没有定义operator = 操作符,即使定义了 *r 只是一个指针也没用空间去存放
xuch318 2006-05-15
  • 打赏
  • 举报
回复
晕,没看完就说了...
xuch318 2006-05-15
  • 打赏
  • 举报
回复
楼主要把程序补齐还是讨论题目的??
molinus 2006-05-15
  • 打赏
  • 举报
回复
up
aronzhou 2006-05-15
  • 打赏
  • 举报
回复
mark

65,209

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧