c++ - 将对象存储在 vector 中,编译器说元素不存在

标签 c++ class inheritance vector

我正在练习使用类和 vector 并将对象存储在 vector 中。在存储了一个基类对象和一个继承自它的类对象后,我的编译器返回了继承类对象不存在的错误。以下是我认为的重要部分:

继承自 Car 的 Car 类和 Sportscar 类

class Car
{
    public:
        Car();
        void stop();
        int get_speed() const;
        void new_speed(int fspeed);

    protected:
        int speed;
};

class Sportscar : public Car
{
    public:
        Sportscar();
        void turbo();

    private:
        bool hasTurbo;
};

将对象存储在 vector 车中:

vector<Car*> cars(2);

    cars[0] = new Car();
    cars[1] = new Sportscar();

访问 cars[0]cars[1] 的情况,其中 cars[0] 访问正常,但是cars[1] 不是:

cars[1]->turbo();
cars[0]->new_speed(cars[0]->get_speed());

这是完整的代码,因为我一开始并不确定我要玩这个小型汽车游戏,所以代码有点乱:

#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;

class Car
{
    public:
        Car();
        void stop();
        int get_speed() const;
        void new_speed(int fspeed);

    protected:
        int speed;
};

class Sportscar : public Car
{
    public:
        Sportscar();
        void turbo();

    private:
        bool hasTurbo;
};

// Constructor for class Car
Car::Car()
{
    speed = 1;
}

// Function to stop the car
void Car::stop()
{
    speed = 0;

    cout << "You stop the car.\n" << endl;
}

// Function to get current speed of the car
int Car::get_speed() const
{
    return speed;
}

// Function to "accelerate" the car
void Car::new_speed(int fspeed)
{
    int newspeed;

    cout << "Speed up by how many mp/h? ";
    cin >> newspeed;

    speed = (fspeed + newspeed);

    cout << "You speed up.\n" << endl;
}

// Sportscar constructor to set hasTurbo to 0 (no)
Sportscar::Sportscar()
    // Not sure if this is necessary
    : Car()
{
    hasTurbo = 0;
}

// Function to designate if the car has turbo
void Sportscar::turbo()
{
    hasTurbo = 1;

    cout << "You have activated turbo and have greater acceleration.\n" << endl;
}

int main(void)
{
    vector<Car*> cars(2);

    cars[0] = new Car();
    cars[1] = new Sportscar();

    //Car drive;
    //Sportscar drivefast;
    char ans;
    char sport;

    // Asks user for input A, B, C, or Q until user chooses Q to quit
    while(1)
    {
        cout << "Enter 'A' for current speed, 'B' to speed up, or 'C' to stop. 'Q' to quit." << endl;
        cin >> ans;

        // If users chooses A, calls get_speed() function and outputs current speed
        if (ans == 'A')
        {
            cout << "Your current speed is " << cars[0]->get_speed() << " mp/h.\n" << endl;
        }

        // If user chooses B, calls new_speed() function to increase speed according to
        // user's specifications
        else if (ans == 'B')
        {
            // Asks user if they have turbo Y/N
            cout << "Do you want to use turbo? Y/N: ";
            cin >> sport;

            // If user does have turbo, hasTurbo is set to 1 (yes) and function
            // new_speed() is called
            if (sport == 'Y')
            {
                cars[1]->turbo();
                cars[0]->new_speed(cars[0]->get_speed());
            }

            // If user does not have turbo, hasTurbo stays at 0 (no) and function
            // new_speed() is called
            else if (sport == 'N')
            {
                cars[0]->new_speed(cars[0]->get_speed());
            }

            // If user answers other can Y or N, tells user input is incorrect
            else
            {
                cout << "Incorrect input.\n" << endl;
            }
        }

        // If user chooses C, calls stop() function and reduces car's speed to 0
        else if (ans == 'C')
        {
            cars[0]->stop();
        }

        // If user chooses Q, program ends
        else if (ans == 'Q')
        {
            exit(0);
        }

        // If user chooses other than A, B, C, or Q, tells user input is incorrect
        else
        {
            cout << "Incorrect input.\n" << endl;
        }
    }
}

最后,这是我的编译器错误:

enter image description here

我应该注意,如果我删除访问 cars[1] 的行,程序会编译并运行良好。

最佳答案

您的 vector 被声明为 Car* 类型的 vector 。 Car 类型的对象没有 turbo 成员函数。您需要将指针转换为 Sportscar* 类型:

static_cast<Sportscar*>(cars[1])->turbo();

关于c++ - 将对象存储在 vector 中,编译器说元素不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25736050/

相关文章:

javascript - 如何拥有一个属于某物属性的类?

使用相同类的 Python/Pygame Sprite 无法正确更新

c++ - 查找基类/父类(super class)成员的实现

java - 像这样使用继承是个坏主意吗

c++ - 谁能帮我理解这个错误? "definition of implicitly-declared ‘classA::classA()’ "

c++ - 在类中重载运算符并返回对私有(private)值的引用

c++ - 基类函数被调用两次

c++ - 谷歌测试和 Visual Studio 2010 RC

Python PyQt 程序结构

c++ - 错误 C2614 : 'ChildClass' : illegal member initialization: 'var1' is not a base or member