-3

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"

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

ParkingLot.cpp:

#pragma once

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


ParkingLot::ParkingLot() {
    for (int i=0; i<5; i++) {
        HandicappedParkingSpace HPS(1, nullptr);
        vacant_handicapparkingspaces.push_back(HPS);
    }

    std::cout<<"finished parking lot"<<std::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
        {
            std::cout<<"No handicapped spaces available"<<std::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_handicappparkingspaces.size()!=0) {
            vacant_handicapparkingspaces.push_back(_ps);
            occupied_handicapparkingspaces.pop_back();
        }
        else {
            std::cout<<"Unable to release any handicapped spaces"<<std::endl;
        }
    }
}

void ParkingLot::getOccupiedSpaces() {
    std::cout<<"Occupied handicap spaces: "<<occupied_handicapparkingspaces.size()<<std::endl;
    std::cout<<"Vacant handicap spaces: "<<vacant_handicapparkingspaces.size()<<std::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;

};

main.cpp

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


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();

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

This gives the following error

ParkingLot.cpp:1:9: warning: #pragma once in main file
 #pragma once
         ^~~~
ParkingLot.cpp: In member function ‘void ParkingLot::ParkVehicle(Vehicle*, ParkingSpace&, std::__cxx11::string)’:
ParkingLot.cpp:34:48: error: no matching function for call to ‘std::vector<HandicappedParkingSpace>::push_back(ParkingSpace&)’
    occupied_handicapparkingspaces.push_back(_ps);
                                                ^
In file included from /usr/include/c++/7/vector:64:0,
                 from ParkingLot.cpp:5:
/usr/include/c++/7/bits/stl_vector.h:939:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = HandicappedParkingSpace; _Alloc = std::allocator<HandicappedParkingSpace>; std::vector<_Tp, _Alloc>::value_type = HandicappedParkingSpace]
       push_back(const value_type& __x)
       ^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:939:7: note:   no known conversion for argument 1 from ‘ParkingSpace’ to ‘const value_type& {aka const HandicappedParkingSpace&}’
/usr/include/c++/7/bits/stl_vector.h:953:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = HandicappedParkingSpace; _Alloc = std::allocator<HandicappedParkingSpace>; std::vector<_Tp, _Alloc>::value_type = HandicappedParkingSpace]
       push_back(value_type&& __x)
       ^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:953:7: note:   no known conversion for argument 1 from ‘ParkingSpace’ to ‘std::vector<HandicappedParkingSpace>::value_type&& {aka HandicappedParkingSpace&&}’
ParkingLot.cpp: In member function ‘void ParkingLot::ReleaseVehicle(Vehicle*, ParkingSpace&, std::__cxx11::string)’:
ParkingLot.cpp:76:46: error: no matching function for call to ‘std::vector<HandicappedParkingSpace>::push_back(ParkingSpace&)’
    vacant_handicapparkingspaces.push_back(_ps);
                                              ^
In file included from /usr/include/c++/7/vector:64:0,
                 from ParkingLot.cpp:5:
/usr/include/c++/7/bits/stl_vector.h:939:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = HandicappedParkingSpace; _Alloc = std::allocator<HandicappedParkingSpace>; std::vector<_Tp, _Alloc>::value_type = HandicappedParkingSpace]
       push_back(const value_type& __x)
       ^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:939:7: note:   no known conversion for argument 1 from ‘ParkingSpace’ to ‘const value_type& {aka const HandicappedParkingSpace&}’
/usr/include/c++/7/bits/stl_vector.h:953:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = HandicappedParkingSpace; _Alloc = std::allocator<HandicappedParkingSpace>; std::vector<_Tp, _Alloc>::value_type = HandicappedParkingSpace]
       push_back(value_type&& __x)
       ^~~~~~~~~
/usr/include/c++/7/bits/stl_vector.h:953:7: note:   no known conversion for argument 1 from ‘ParkingSpace’ to ‘std::vector<HandicappedParkingSpace>::value_type&& {aka HandicappedParkingSpace&&}’

Can anyone help with this?

EDIT:

Forgot to include CompactParkingSpace. ParkingLot.h should be :

#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 <CompactParkingSpace> occupied_compactparkingspaces;
        std::vector <CompactParkingSpace> vacant_compactparkingspaces;
};

ParkingLot.cpp should be

#pragma once

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


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);
    }
    std::cout<<"finished parking lot"<<std::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
        {
            std::cout<<"No handicapped spaces available"<<std::endl;
        }
    }

    else if  (ps=="Compact") {
        if (vacant_compactparkingspaces.size()!=0) {
            _ps.parkvehicle(_v);
            vacant_compactparkingspaces.pop_back();
            occupied_compactparkingspaces.push_back(_ps);
        }
        else
        {
            std::cout<<"No compact spaces available"<<std::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_handicapparkingspaces.size()!=0) {
            vacant_handicapparkingspaces.push_back(_ps);
            occupied_handicapparkingspaces.pop_back();
        }
        else {
            std::cout<<"Unable to release any handicapped spaces"<<std::endl;
        }
    }

    else if  (ps=="Compact") {
        if (occupied_compactparkingspaces.size()!=0) {
            _ps.parkvehicle(_v);
            vacant_compactparkingspaces.push_back(_ps);
            occupied_compactparkingspaces.pop_back();
        }
        else {
            std::cout<<"Unable to release any compact spaces"<<std::endl;
        }
    }

}

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

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;


};
user5739619
  • 1,748
  • 5
  • 26
  • 40
  • 5
    `occupied_handicapparkingspaces` is a `std::vector`, not a `std::vector`. You cannot `push_back` a `ParkingSpace` value, because no implicit constructor exists to convert `ParkingSpace` to `HandicappedParkingSpace`. This is what the error message is telling you. – paddy Mar 06 '19 at 00:51
  • 1
    Extending Paddy's comment. `HandicappedParkingSpace` is a `ParkingSpace`, but this relationship does not extend to the reverse. Not all `ParkingSpace`s are `HandicappedParkingSpace`s. You can put a `HandicappedParkingSpace` variable into a `ParkingSpace` variable, but you will have to take [Object Slicing](https://stackoverflow.com/questions/274626/what-is-object-slicing) into account. – user4581301 Mar 06 '19 at 01:16
  • @paddy Just to clarify, I wrote the code this way because I wanted `ParkVehicle` to perform `vacant_handicapparkingspaces.pop_back()` if a `HandicappedParkingSpace` object is passed into it, or a `vacant_compactparkingspaces.pop_back()` if a `CompactParkingSpace` object is passed into it. See the EDIT – user5739619 Mar 06 '19 at 01:18
  • Also, as the very first warning indicates, #pragma once doesn't go in .cpp files, only in headers as a header guard to prevent multiple inclusions in the same translation unit. – Phil M Mar 06 '19 at 01:21
  • Compact (and Handicapped) ParkingSpaces, if you're going to make them derive from `ParkingSpace`, should not need their own copies of `vacant` and `vehicle`; those should be `protected` members in `ParkingSpace` instead of `private`. – Phil M Mar 06 '19 at 01:25
  • @user5739619 okay sure, but you're doing it wrong. I'm just interpreting the compiler error for you. You can't argue to the compiler that you meant something different and have it just decide "oh okay I will compile your program based on your intent, rather than what you wrote". You _could_ use `dynamic_cast`, or you _could_ create function overloads. You _shouldn't_ need the extra string to say what type of object you're supplying. And it's _confusing_ that the parking lot allows callers to provide their own parking spaces that don't exist in `ParkingLot`. – paddy Mar 06 '19 at 02:07
  • That is... a lot of code to wade through. Next time, to help the people helping you, you may want to focus on the "minimal" part of [mcve]. – JaMiT Mar 06 '19 at 03:37

1 Answers1

1

While there is quite a lot wrong with the code, the first error you're getting

ParkingLot.cpp: In member function ‘void ParkingLot::ParkVehicle(Vehicle*, 
ParkingSpace&, std::__cxx11::string)’:
ParkingLot.cpp:34:48: error: no matching function for call to 
‘std::vector<HandicappedParkingSpace>::push_back(ParkingSpace&)’
occupied_handicapparkingspaces.push_back(_ps);

is because you are trying to push a ParkingSpace into a vector that only takes HandicappedParkingSpaces. If you were storing pointers, then you could push a HandicappedParkingSpace* into a vector holding ParkingSpace*s, but (as other commenters said) even then you couldn't do the opposite.

You might want to scour this list for some books to help you get a better grasp on inheritance.

Phil M
  • 1,619
  • 1
  • 8
  • 10