HandicappedParkingSpace

HandicappedParkingSpace

我有以下代码

Vehicle.h

#pragma once
class Vehicle
{
    public:
        Vehicle();
        ~Vehicle();
    private:
        int wheels;
};


Car.h

#pragma once
#include "Vehicle.h"
class Car: public Vehicle
{
    public:
        Car();
        ~Car();
    private:
        int wheels=4;
};


ParkingLot.h

#pragma once
#include <vector>
#include <string>
#include "ParkingSpace.h"
#include "HandicappedParkingSpace.h"
#include "CompactParkingSpace.h"
#include "RegularParkingSpace.h"
class ParkingLot
{
    public:
        ParkingLot();
        ~ParkingLot();
        void ParkVehicle(Vehicle _v, ParkingSpace& _ps, std::string ps);
        void ReleaseVehicle(Vehicle _v, ParkingSpace& _ps, std::string ps);
        void getOccupiedSpaces();
    private:
        int value;
        std::vector <HandicappedParkingSpace> occupied_handicapparkingspaces;
        std::vector <HandicappedParkingSpace> vacant_handicapparkingspaces;
        std::vector <RegularParkingSpace> occupied_regularparkingspaces;
        std::vector <RegularParkingSpace> vacant_regularparkingspaces;
        std::vector <CompactParkingSpace> occupied_compactparkingspaces;
        std::vector <CompactParkingSpace> vacant_compactparkingspaces;
};


ParkingLot.cpp

#pragma once

#include <iostream>
#include <string>
#include <vector>
#include "ParkingLot.h"

using namespace std

ParkingLot::ParkingLot() {
    for (int i=0; i<5; i++) {
        HandicappedParkingSpace HPS(1, Null);
        vacant_handicapparkingspaces.push_back(HPS);
    }
    for (int i=0; i<5; i++) {
        CompactParkingSpace CPS(1, Null);
        vacant_compactparkingspaces.push_back(CPS);
    }
    for (int i=0; i<5; i++) {
        RegularParkingSpace RPS(1, Null);
        vacant_regularparkingspaces.push_back(RPS);
    }
    cout<<"finished parking lot"<<endl;
}


void ParkingLot::ParkVehicle(Vehicle _v, ParkingSpace& _ps, std::string ps)
{
    if (ps=="Handicapped") {
        if (vacant_handicapparkingspaces.size()!=0) {
            _ps.parkvehicle(_v);
            vacant_handicapparkingspaces.pop_back();
            occupied_handicapparkingspaces.push_back(_ps);
        }
        else
        {
            cout<<"No handicapped spaces available"<<endl;
        }
    }
    else if  (ps=="Compact") {
        if (vacant_compactparkingspaces.size()!=0) {
            _ps.parkvehicle(_v);
            vacant_compactparkingspaces.pop_back();
            occupied_compactparkingspaces.push_back(_ps);
        }
        else
        {
            cout<<"No compact spaces available"<<endl;
        }
    }
    else {
        if (vacant_regularparkingspaces.size()!=0) {
            _ps.parkvehicle(_v);
            vacant_regularparkingspaces.pop_back();
            occupied_regularparkingspaces.push_back(_ps);
        }
        else {
            cout<<"No regular spaces available"<<endl;
        }
    }

}

void ParkingLot::ReleaseVehicle(Vehicle _v, ParkingSpace& _ps, std::string ps)
{
    _ps.vacant=1;
    _ps.vehicle= Null;
    if (ps=="Handicapped") {
        if (occupied_regularparkingspaces.size()!=0) {
            vacant_handicapparkingspaces.push_back(_ps);
            occupied_handicapparkingspaces.pop_back();
        }
        else {
            cout<<"Unable to release any handicapped spaces"<<endl;
        }
    }
    else if  (ps=="Compact") {
        if (occupied_regularparkingspaces.size()!=0) {
            _ps.parkvehicle(_v);
            vacant_compactparkingspaces.pop_back(_ps);
            occupied_compactparkingspaces.push_back();
        }
        else {
            cout<<"Unable to release any compact spaces"<<endl;
        }
    }
    else {
        if (occupied_regularparkingspaces.size()!=0) {
            _ps.parkvehicle(_v);
            vacant_regularparkingspaces.pop_back(_ps);
            occupied_regularparkingspaces.push_back();
        }
        else {
            cout<<"Unable to release any regular spaces"<<endl;
        }
    }
}

void ParkingLot::getOccupiedSpaces() {
    cout<<"Occupied handicap spaces: "<<occupied_handicapparkingspaces.size()<<endl;
    cout<<"Vacant handicap spaces: "<<vacant_handicapparkingspaces.size()<<endl;
    cout<<"Occupied compact spaces: "<<occupied_compactparkingspaces.size()<<endl;
    cout<<"Vacant compact spaces: "<<vacant_compactparkingspaces.size()<<endl;
    cout<<"Occupied regular spaces: "<<occupied_regularparkingspaces.size()<<endl;
}


ParkingSpace.h

#pragma once

#include "Vehicle.h"

class ParkingSpace
{
    public:
        ParkingSpace();
        ~ParkingSpace();
        virtual void parkvehicle()=0;


    private:
        Vehicle *vehicle;
        bool vacant;

};


HandicappedParkingSpace.h

#pragma once

#include "ParkingSpace.h"

class HandicappedParkingSpace : public ParkingSpace
{
    public:
        HandicappedParkingSpace(int _vacant, Vehicle* _v) {
            this->vacant=_vacant;
            this->vehicle=_v;
        }

        ~HandicappedParkingSpace();

        void parkvehicle(Vehicle _v) {
            this->vacant=0;
            this->vehicle=_v;
        }

    private:
        int vacant;
        Vehicle *vehicle;

};


RegularParkingSpace.h

#pragma once

#include "ParkingSpace.h"

class RegularParkingSpace : public ParkingSpace
{
    public:
        RegularParkingSpace(int _vacant, Vehicle* _v) {
            this->vacant=_vacant;
            this->vehicle=_v;
        }
        ~RegularParkingSpace();
        void parkvehicle(Vehicle _v) {
            this->vacant=0;
            this->vehicle=_v;
        }

    private:
        int vacant;
        Vehicle *vehicle;


};


CompactParkingSpace.h

#pragma once

#include "ParkingSpace.h"

class CompactParkingSpace : public ParkingSpace
{
    public:
        CompactParkingSpace(int _vacant, Vehicle* _v) {
            this->vacant=_vacant;
            this->vehicle=_v;
        }
        ~CompactParkingSpace();

        void parkvehicle(Vehicle _v) {
            this->vacant=0;
            this->vehicle=_v;

        }

    private:
        int vacant;
        Vehicle *vehicle;


};


main.cpp

#include "ParkingLot.h"
#include "HandicappedParkingSpace.h"
#include "RegularParkingSpace.h"
#include "CompactParkingSpace.h"
#include "Car.h"
#include <iostream>

using namespace std;

int main()
{
    ParkingLot PL;
    Car c1;
    HandicappedParkingSpace HPS;
    PL.ParkVehicle(c1, HPS, "Handicapped");
    Car c2;
    CompactParkingSpace CPS;
    PL.ParkVehicle(c2, CPS, "Handicapped");

    PL.getOccupiedSpaces();

    cout<<"FINISHED"<<endl;
    //delete d;
        return 0;
}


这是我得到的错误:https://pastebin.com/p0vzb0Mz(错误很长,我无法在此处发布)

有人能帮忙吗?

编辑:

我修改了ParkingLot.cpp,所以现在看起来像

#pragma once

#include <iostream>
#include <string>
#include <vector>
#include "ParkingLot.h"

using namespace std;


ParkingLot::ParkingLot() {
    for (int i=0; i<5; i++) {
        HandicappedParkingSpace HPS(1, nullptr);
        vacant_handicapparkingspaces.push_back(HPS);
    }
    for (int i=0; i<5; i++) {
        CompactParkingSpace CPS(1, nullptr);
        vacant_compactparkingspaces.push_back(CPS);
    }
    for (int i=0; i<5; i++) {
        RegularParkingSpace RPS(1, nullptr);
        vacant_regularparkingspaces.push_back(RPS);
    }
    cout<<"finished parking lot"<<endl;
}


void ParkingLot::ParkVehicle(Vehicle _v, ParkingSpace& _ps, std::string ps)
{
    if (ps=="Handicapped") {
        if (vacant_handicapparkingspaces.size()!=0) {
            _ps.parkvehicle(_v);
            vacant_handicapparkingspaces.pop_back();
            occupied_handicapparkingspaces.push_back(_ps);
        }
        else
        {
            cout<<"No handicapped spaces available"<<endl;
        }
    }
    else if  (ps=="Compact") {
        if (vacant_compactparkingspaces.size()!=0) {
            _ps.parkvehicle(_v);
            vacant_compactparkingspaces.pop_back();
            occupied_compactparkingspaces.push_back(_ps);
        }
        else
        {
            cout<<"No compact spaces available"<<endl;
        }
    }
    else {
        if (vacant_regularparkingspaces.size()!=0) {
            _ps.parkvehicle(_v);
            vacant_regularparkingspaces.pop_back();
            occupied_regularparkingspaces.push_back(_ps);
        }
        else {
            cout<<"No regular spaces available"<<endl;
        }
    }

}


void ParkingLot::ReleaseVehicle(Vehicle _v, ParkingSpace& _ps, std::string ps)
{
    //_ps.vacant=1;
    //_ps.vehicle= nullptr;
    _ps.setVehicle(1, nullptr);

    if (ps=="Handicapped") {
        if (occupied_regularparkingspaces.size()!=0) {
            vacant_handicapparkingspaces.push_back(_ps);
            occupied_handicapparkingspaces.pop_back();
        }
        else {
            cout<<"Unable to release any handicapped spaces"<<endl;
        }
    }
    else if  (ps=="Compact") {
        if (occupied_regularparkingspaces.size()!=0) {
            _ps.parkvehicle(_v);
            vacant_compactparkingspaces.push_back(_ps);
            occupied_compactparkingspaces.pop_back();
        }
        else {
            cout<<"Unable to release any compact spaces"<<endl;
        }
    }
    else {
        if (occupied_regularparkingspaces.size()!=0) {
            _ps.parkvehicle(_v);
            vacant_regularparkingspaces.push_back(_ps);
            occupied_regularparkingspaces.pop_back();
        }
        else {
            cout<<"Unable to release any regular spaces"<<endl;
        }
    }
}

void ParkingLot::getOccupiedSpaces() {
    cout<<"Occupied handicap spaces: "<<occupied_handicapparkingspaces.size()<<endl;
    cout<<"Vacant handicap spaces: "<<vacant_handicapparkingspaces.size()<<endl;
    cout<<"Occupied compact spaces: "<<occupied_compactparkingspaces.size()<<endl;
    cout<<"Vacant compact spaces: "<<vacant_compactparkingspaces.size()<<endl;
    cout<<"Occupied regular spaces: "<<occupied_regularparkingspaces.size()<<endl;
    cout<<"Vacant regular spaces: "<<vacant_regularparkingspaces.size()<<endl;
}


ParkingSpace.h现在

#pragma once

#include "Vehicle.h"

class ParkingSpace
{
    public:
        ParkingSpace();
        ~ParkingSpace();
        virtual void parkvehicle(Vehicle _v)=0;
        virtual void setVehicle(bool vacant, Vehicle _v);


    private:
        Vehicle vehicle;
        bool vacant;

};


HandicappedParkingSpace.h现在

#pragma once

#include "ParkingSpace.h"

class HandicappedParkingSpace : public ParkingSpace
{
    public:
        HandicappedParkingSpace(int _vacant, Vehicle& _v) {
            this->vacant=_vacant;
            this->vehicle=_v;
        }

        ~HandicappedParkingSpace();

        void parkvehicle(Vehicle _v) {
            this->vacant=0;
            this->vehicle=_v;
        }

        void setVehicle(bool _vacant, Vehicle _v) {
            this->vacant=_vacant;
            this->vehicle= _v;
        }


    private:
        int vacant;
        Vehicle vehicle;

};


RegularParkingSpace.h现在

#pragma once

#include "ParkingSpace.h"

class RegularParkingSpace : public ParkingSpace
{
    public:
        RegularParkingSpace(int _vacant, Vehicle& _v) {
            this->vacant=_vacant;
            this->vehicle=_v;
        }
        ~RegularParkingSpace();
        void parkvehicle(Vehicle _v) {
            this->vacant=0;
            this->vehicle=_v;
        }

        void setVehicle(bool _vacant, Vehicle _v) {
            this->vacant=_vacant;
            this->vehicle= _v;
        }

    private:
        int vacant;
        Vehicle vehicle;


};


CompactParkingSpace.h现在

#pragma once

#include "ParkingSpace.h"

class CompactParkingSpace : public ParkingSpace
{
    public:
        CompactParkingSpace(int _vacant, Vehicle& _v) {
            this->vacant=_vacant;
            this->vehicle=_v;
        }
        ~CompactParkingSpace();

        void parkvehicle(Vehicle _v) {
            this->vacant=0;
            this->vehicle=_v;

        }

        void setVehicle(bool _vacant, Vehicle _v) {
            this->vacant=_vacant;
            this->vehicle= _v;
        }

    private:
        int vacant;
        Vehicle vehicle;


};


main.cpp现在

#include "ParkingLot.h"
#include "HandicappedParkingSpace.h"
#include "RegularParkingSpace.h"
#include "CompactParkingSpace.h"
#include "Car.h"
#include <iostream>

using namespace std;

int main()
{
    ParkingLot PL;
    Car c1;
    HandicappedParkingSpace HPS(1, nullptr);
    PL.ParkVehicle(c1, HPS, "Handicapped");
    Car c2;
    CompactParkingSpace CPS(1, nullptr);
    PL.ParkVehicle(c2, CPS, "Handicapped");

    PL.getOccupiedSpaces();

    cout<<"FINISHED"<<endl;
    //delete d;
        return 0;
}


但是现在我得到了这些错误,看来它们大多与nullptr有关:https://pastebin.com/hVdcSc63

我以为push_back错误是因为我需要将std:vector之类的occupied_handicapparkingspaces<HandicappedParkingSpace>更改为<HandicappedParkingSpace&>,然后出现此错误:https://pastebin.com/QkWC6SRk

最佳答案

无法在C ++中为停车场OOP声明抽象类型的变量


由于virtual void parkvehicle()=0;,ParkingSpace是一个抽象类,HandicappedParkingSpace继承了ParkingSpace而未定义parkvehicle,因此它也是一个抽象类,因此您无法实例化HandicappedParkingSpace,这就是为什么出现有关HandicappedParkingSpace HPS(1, Null);和等效情况的错误的原因

注意还必须将NULL替换为NULL或更佳的nullptr,并且您还有很多其他错误,我只是回答您的标题...



编辑后

我建议您不要混用using namespace std;std::xxx,如果使用std前缀,请删除所有使用的内容


  ParkingLot.cpp:13:41:错误:没有匹配的函数可调用“ HandicappedParkingSpace :: HandicappedParkingSpace(int,std :: nullptr_t)”
         残障人士停车位HPS(1,nullptr);


HandicappedParkingSpace的构造函数的第二个参数的类型为Vehicle&,与nulltr不兼容,您混淆了Vehicle&Vehicle*,只有指针可以将nulltr赋值


  我以为push_back错误是因为我需要将std:vectors(例如,占用的手掌停车空间)从<HandicappedParkingSpace>更改为<HandicappedParkingSpace&>


不,问题是您在occupied_handicapparkingspaces.push_back(_ps);ps_时执行了std::string,而std::string不是HandicappedParkingSpace,并且没有从std::string到HandicappedParkingSpace的转换

关于c++ - 无法在C++中为 parking 场OOP声明抽象类型的变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54997049/

10-13 03:31