У меня есть следующий код
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
s, например occupied_handicapparkingspaces
с <HandicappedParkingSpace>
на <HandicappedParkingSpace&>
, тогда я получаю эту ошибку: https://pastebin.com/QkWC6SRk