0

I have the following code

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;
}

This is the error I get: https://pastebin.com/p0vzb0Mz (the error was so long I wasn't able to post it here)

Can anyone help with this?

EDIT:

I modified ParkingLot.cpp so it now looks like

#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 is now

#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 is now

#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 is now

#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 is now

#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 is now

#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;
}

But now I get these errors, it seems they're mostly with the nullptr: https://pastebin.com/hVdcSc63

I thought the push_back errors were because I needed to change the std:vectors such as occupied_handicapparkingspaces from <HandicappedParkingSpace> to <HandicappedParkingSpace&>, then I get this error: https://pastebin.com/QkWC6SRk

user5739619
  • 1,748
  • 5
  • 26
  • 40
  • 2
    Have you tried reading the errors one by one? 20% are about writing `Null` (which does not exist) insteadof `nullptr` or `NULL`. Another 30% are about `parkVehicle`, which does NOT take an argument in your base class, but does in the subclasses. As this method is not overridden, your subclasses are also considered abstract, as the compiler says. To help yourself identify this kind of errors, put the `override` keyword on overriding methods in the subclasses (ie `void parkVehicle(Vehicle _v) override` in CompactParkingSpace. – Botje Mar 05 '19 at 07:04
  • 1
    `class Vehicle { int wheels; }; class Car : public Vehicle { int wheels=4; };` – but you *are* aware that `Vehicle`'s `wheels` are not the same as `Car`'s ones are, are you? – Aconcagua Mar 05 '19 at 07:09
  • About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Mar 05 '19 at 07:11
  • Offtopic: A matter of design: Having different vectors for vacant and occupied parking lots appears questionable to me. Spontaneously, I'd rather place both into the same vector and rather add a flag `isOccupied` to the base class. This avoids the necessity of moving the lots around in the vectors (and preserves their order, if that's of interest). Of course, that's use-case-dependent; if your main goal is just to find free parking lots as quickly as possible, then your approach might be superior... – Aconcagua Mar 05 '19 at 07:21
  • I replaced the `Null` with `nullptr`s and got new errors as mentioned in the `EDIT` – user5739619 Mar 05 '19 at 07:40
  • @Aconcagua I thought about that too, but I just want to make sure my code works first and I understand where these current errors are coming from and how to fix them – user5739619 Mar 05 '19 at 07:41
  • Your issue with nullptr is you changed your constructors for CompactParkingSpace to take references. In your original code it was pointers. That is an example of one of the nullptr issues. I would recommend you learn how to read compiler errors. – Mobile Ben Mar 05 '19 at 07:59

1 Answers1

1

cannot declare variable of abstract type for Parking Lot OOP in C++

ParkingSpace is an abstract class because of virtual void parkvehicle()=0;, HandicappedParkingSpace inherits ParkingSpace without defining parkvehicle so it is also an abstract class, then you cannot instantiate HandicappedParkingSpace and this why you have the error about HandicappedParkingSpace HPS(1, Null); and equivalent cases

Note also Null must be replaced by NULL or better nullptr, and you have plenty other errors, I just answer about your title ...


After your edit

I encourage you to not mix using namespace std; and std::xxx, remove all your using if you use the std prefix

ParkingLot.cpp:13:41: error: no matching function for call to ‘HandicappedParkingSpace::HandicappedParkingSpace(int, std::nullptr_t)’ HandicappedParkingSpace HPS(1, nullptr);

The type of the second parameter of the constructor of HandicappedParkingSpace is Vehicle&, nulltr is not compatible with, you confuse Vehicle& and Vehicle*, only a pointer can values nulltr

I thought the push_back errors were because I needed to change the std:vectors such as occupied_handicapparkingspaces from <HandicappedParkingSpace> to <HandicappedParkingSpace&>

no, the problem is you do occupied_handicapparkingspaces.push_back(_ps); while ps_ is a std::string, a std::string is not a HandicappedParkingSpace and there is no conversion from a std::string to a HandicappedParkingSpace

bruno
  • 32,421
  • 7
  • 25
  • 37
  • 3
    Better: `nullptr` – C++ *keywords* should be preferred over old (obsolete?) C *macros*. – Aconcagua Mar 05 '19 at 07:12
  • I replaced the `Null` with `nullptr`s and got new errors as mentioned in the `EDIT` – user5739619 Mar 05 '19 at 07:40
  • 1
    @user5739619 sure you have other errors, as I said I only answer to your title – bruno Mar 05 '19 at 07:42
  • @user5739619 I edited my answer to speak about some of your other problems, but we cannot continue like that, this is not the initial question, please accept my answer to put the question solved, look at my adding and try to solve your problem by yourself. If you do not understand some errors open a new question. – bruno Mar 05 '19 at 08:18
  • "HandicappedParkingSpace inherits ParkingSpace without defining parkvehicle ". I don't understand this. Isn't it defined in the original `HandicappedParkingSpace` under `void parkvehicle(Vehicle _v) { this->vacant=0; this->vehicle=_v; } `? – user5739619 Mar 05 '19 at 16:44
  • @user5739619 no, in _ParkingSpace_ the profile was`virtual void parkvehicle()` so not the same as `void parkvehicle(Vehicle _v)` is _HandicappedParkingSpace_, this is not the same method, the name is not enough. I was speaking about your original question before your edit – bruno Mar 05 '19 at 17:25