我有以下代码:
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;
}
这会出现以下错误
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&&}’
任何人都可以帮忙吗?
编辑:
忘记包含CompactParkingSpace
。 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 <CompactParkingSpace> occupied_compactparkingspaces;
std::vector <CompactParkingSpace> vacant_compactparkingspaces;
};
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);
}
/*
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;
};
答案 0 :(得分:1)
虽然代码有很多错误,但您遇到的第一个错误
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);
是因为您试图将ParkingSpace
推入仅需要HandicappedParkingSpace
的向量中。如果要存储指针,则可以将HandicappedParkingSpace*
推入包含ParkingSpace*
s的向量中,但是(如其他评论者所说)即使如此,您也无法做到相反。
您可能希望搜寻this list一些书籍,以帮助您更好地了解继承。