如何将矢量对象传递给虚函数
How to pass vector object to a virtual function?
谁能告诉我如何将向量传递给虚函数?当我尝试编译时,我收到一些错误,例如"Car":未声明的标识符和"Car"无法实例化抽象类?有人猜我做错了什么吗?
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
class Handling
{
public:
virtual void add_car() = 0;
virtual void erase_car() = 0;
virtual void show_car(vector<Car> _carvector) = 0;
};
class Car : virtual public Handling
{
public:
string manufacturer;
string model;
string type;
int engine;
string enginetype;
int yearofproduction;
string geartype;
int noofgears;
string price;
_manufacturer = "Opel";
_model = "Astra";
_type = "Hatchback";
_engine = 1900;
_enginetype = "Diesel";
_yearofproduction = 1999;
_geartype = "Manual";
_noofgears = 5;
_price = "5600";
Car(){};
Car(string _manufacturer, string _model, string _type, int _enigne, string _enginetype, int _yearofproduction, string _geartype, int _noofgears, string _price);
void add_car()
{
}
void erase_car()
{
}
void show_car(vector<Car> _carvector)
{
for (int i = 0; i < _carvector.size(); i++)
{
cout << "Samochod nr. " << i + 1 << endl;
cout << "------------------------------" << endl;
cout << "Marka: " << _carvector[i].manufacturer << endl;
cout << "Model: " << _carvector[i].model << endl;
cout << "Typ: " << _carvector[i].type << endl;
cout << "Pojemnosc silnika: " << _carvector[i].engine << " cm3" << endl;
cout << "Rodzaj silnika: " << _carvector[i].enginetype << endl;
cout << "Rok produkcji: " << _carvector[i].yearofproduction << endl;
cout << "Rodzaj skrzyni biegow: " << _carvector[i].geartype << endl;
cout << "Liczba biegow: " << _carvector[i].noofgears << endl;
cout << "Cena: " << _carvector[i].price << " PLN" << endl;
cout << endl;
}
}
};
Car::Car(string _manufacturer, string _model, string _type, int _engine, string _enginetype, int _yearofproduction, string _geartype, int _noofgears, string _price)
: manufacturer(_manufacturer)
, model(_model)
, type(_type)
, engine(_engine)
, enginetype(_enginetype)
, yearofproduction(_yearofproduction)
, geartype(_geartype)
, noofgears(_noofgears)
, price(_price)
{
}
int main()
{
vector<Car> carvector;
Car carobject;
string _manufacturer;
string _model;
string _type;
int _engine;
string _enginetype;
int _yearofproduction;
string _geartype;
int _noofgears;
string _price
carvector.push_back(Car(_manufacturer, _model, _type, _engine, _enginetype,_yearofproduction, _geartype, _noofgears, _price));
carobject.show_car(carvector);
在 Handling 类声明之前放置的那一小行代码就是这样做的。
class Car;
相关文章:
- 如何使用单独文件中的派生类访问友元函数对象
- 当使用透明的std函数对象时,我们还需要写空的尖括号吗
- 有没有办法将重载的类函数绑定到函数对象?
- 将指针传递到成员的指针,从模板参数包到函数对象
- 如何在类模板的成员函数中正确调用函数对象?正在生成 Visual Studio 编译器错误 C2440
- 隐式转换为比较函数对象(函子)用于 std::sort 而不是 std::map?
- C++使用函数对象的线程,如何调用多个析构函数而不是构造函数?
- 如何通过接口将函子分配给函数对象
- 对std::函数对象的调用不匹配,该对象是指向成员函数的指针
- 指向std::invoke中成员函数对象的指针
- 如何成功地将函数对象(或lambda)传递给trackbar回调的第二个参数(void*)
- "std::function"的简单版本:函数对象的生存期?
- C++ 将函数对象作为左值和/或右值传递
- SFINAE 用于具有默认参数的函数对象
- 构造函数对象赋值是否泄漏内存
- 如何发送通过绑定到函数/方法创建的函数对象?
- std::for_each 与函数对象
- 将函数对象传递给 std::function
- 访问执行策略for_each函数对象中的迭代器
- 通过C++函数对象类访问参数