【问题标题】:How to pass vector object to a virtual function?如何将向量对象传递给虚函数?
【发布时间】:2015-01-13 18:34:39
【问题描述】:

谁能告诉我如何将向量传递给虚函数?当我尝试编译时,会出现一些错误,例如“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);

【问题讨论】:

  • 第二个错误可能是第一个错误引起的。
  • 1.预申报车。 2. 将class Car 中的show_car 设为虚拟。 3. 有一个父类依赖于一个像这样的子类会破坏Dependency Inversion Principle 所以扔掉这个设计再试一次。
  • 另外,在它成为习惯之前摆脱它:using namespace std - 而不是std::vector&lt;Car&gt;
  • 您有一个基接口Handling,它引用派生类型Car。这通常表明设计有问题,即使您可以修复编译器错误,您也会在尝试正确使用这些类时遇到更高级别的问题。
  • @wisniowy - class Car : virtual public Handling 所以Car 是-a Handling?这对你有意义吗?

标签: c++ function class vector virtual


【解决方案1】:

在处理类声明之前放置的那一小行代码完成了这项工作。

class Car;

【讨论】:

    猜你喜欢
    • 2015-03-07
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 2016-11-09
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    相关资源
    最近更新 更多