main.cpp:4:10: 致命错误: Car.h: 没有那个文件或目录

时间:2021-05-06 17:13:06

标签: linux c++

我正在使用 zybook,它需要我创建一个程序来维护汽车租赁公司的库存。

这个程序的三个文件是:

car.h - 类声明
car.cpp - 类定义
main.cpp - main() 函数和其他描述的函数

当我编写程序时,出现错误:

main.cpp:4:10: fatal error: Car.h: No such file or directory
    4 | #include "Car.h"
      |          ^~~~~~~
compilation terminated.
car.cpp:3:10: fatal error: Car.h: No such file or directory
    3 | #include "Car.h"
      |          ^~~~~~~
compilation terminated.

这是我的代码:

#include <iostream>
#include <string.h>
#include <vector>
#include "Car.h"
#include <algorithm>

using namespace std;

void addCar(vector<Car>& cars);
//bool deleteCar(vector<Car>& cars);
bool updateCarCondition(vector<Car>& cars);
void displayCar(vector<Car>& cars);
void displayAllCars(vector<Car>& cars);
bool rentCar(vector<Car>& cars);
bool returnCar(vector<Car>& cars);

int main(){
    char option;
    vector<Car> cars;

    while (true){
        cout << "CAR RENTAL AGENCY MENU" << endl;
        cout << "a - Add car to inventory" << endl;
        cout << "d - Delete car by id from inventory" << endl;
        cout << "u - Update car by id condition in inventory" << endl;
        cout << "s - Display one car by id from inventory" << endl;
        cout << "i - Display list of all cars in inventory" << endl;
        cout << "c - Rent a car by id in inventory" << endl;
        cout << "r - Return a car by id in inventory" << endl;
        cout << "q - Quit" << endl;

        cout << "Choose an option: " << endl;
        cin >> option;
        cin.ignore();
        cout << endl;

        switch(option){
            case 'a': addCar(cars);
                      break;

            //case 'd': deleteCar(cars);
                      //break;

            case 'u': updateCarCondition(cars);
                      break;

            case 's': displayCar(cars);
                      break;

            case 'i': displayAllCars(cars);
                      break;

            case 'c': rentCar(cars);
                      break;

            case 'r': returnCar(cars);
                      break;

            case 'q': break;

            default: cout << "Please enter a valid option" << endl;
        }
    }

    return 0;
}

void addCar(vector<Car>& cars){
    int id, year;
    string model, make, condition;

    cout << "ADD CAR TO INVENTORY" << endl;

    cout << "Enter an ID: " << endl;
    cin >> id;
    cout << "Enter the make: " << endl;
    cin >> make;
    cout << "Enter the model: " << endl;
    cin >> model;
    cout << "Enter the year: " << endl;
    cin >> year;
    cout << "Enter the condition (new, slighty_used, used): " << endl;
    cin >> condition;

    Car car(id, make, model, year, condition);

    cars.push_back(car);
}

/*bool deleteCar(vector<Car>& cars){
    cout << "REMOVE CAR FROM INVENTORY" << endl;

    int id;

    cout << "Enter the ID of the car to delete: " << endl;
    cin >> id;

    vector<Car>::iterator it;

    for (int i = 0; i < cars.size(); i++){
        if (cars.at(i).Getid() == id){
            //Car const& const_car = cars.at(i);
            //std::vector<Car>::iterator itr = std::find(cars.begin(), cars.end(), const_car);
            it = cars.at(i);
            cars.erase(it);
            return true;
        }
    }

    return false;
}*/

bool updateCarCondition(vector<Car>& cars){
    cout << "UPDARE CAR CONDITION IN INVENTORY" << endl;

    int id;
    string condition;

    cout << "Enter the ID of the car to update condition: " << endl;
    cin >> id;

    cout << "Enter the condition (new, slighty_used, used): " << endl;
    cin >> condition;

    for (int i = 0; i < cars.size(); i++){
        if (cars.at(i).Getid() == id){
            cars.at(i).Setcondition(condition);
            return true;
        }
    }

    return false;
}

void displayCar(vector<Car>& cars){
    cout << "DISPLAY CAR IN INVENTORY" << endl;

    int id;

    cout << "Enter the ID of the car to display: " << endl;
    cin >> id;

    for (int i = 0; i < cars.size(); i++){
        if (cars.at(i).Getid() == id){
            cars.at(i).displayCar();
            break;
        }
    }
}

void displayAllCars(vector<Car>& cars){
    cout << "DISPLAY ALL CARS IN INVENTORY" << endl;

    for (int i = 0; i < cars.size(); i++){
        cars.at(i).displayCar();
    }
}

bool rentCar(vector<Car>& cars){
    cout << "RENT CAR IN INVENTORY" << endl;

    int id;

    cout << "Enter the ID of the car to rent: " << endl;
    cin >> id;

    for (int i = 0; i < cars.size(); i++){
        if (cars.at(i).Getid() == id){
            if (cars.at(i).Getrented()){
                cout << "Car is already rented" << endl;
                return false;
            }

            else if (cars.at(i).Getrented() == false){
                cout << "Car has been successfully rented to you" << endl;
                cars.at(i).toggleRented();
                return true;
            }
        }
    }

    cout << "Car " << id << " not found in inventory" << endl;
    return false;
}

bool returnCar(vector<Car>& cars){
    cout << "RENT CAR TO INVENTORY" << endl;

    int id;

    cout << "Enter the ID of the car to return: " << endl;
    cin >> id;

    for (int i = 0; i < cars.size(); i++){
        if (cars.at(i).Getid() == id){
            if (cars.at(i).Getrented()){
                cars.at(i).toggleRented();
                cout << "Car returned successfully!!" << endl;
                return true;
            }
        }
    }

    cout << "Car " << id << " not found in inventory" << endl;
    return false;
}

0 个答案:

没有答案