将对象存储在向量中,编译器说元素不存在
Posted
技术标签:
【中文标题】将对象存储在向量中,编译器说元素不存在【英文标题】:Storing objects in a vector, compiler says an element does not exist 【发布时间】:2014-09-09 02:56:23 【问题描述】:我正在练习使用类和向量并将对象存储在向量中。在存储了一个基类对象和一个继承自它的类对象后,我的编译器返回了继承的类对象不存在的错误。以下是我认为重要的部分:
继承自 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<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;
最后,这是我的编译器错误:
我应该注意,如果我删除访问cars[1]
的行,程序编译并运行良好。
【问题讨论】:
【参考方案1】:您的向量被声明为Car*
类型的向量。 Car
类型的对象没有 turbo
成员函数。您需要将指针转换为 Sportscar*
类型:
static_cast<Sportscar*>(cars[1])->turbo();
【讨论】:
注意,如果cars[1]
实际上不是Sportscar
,这会导致未定义的行为。它还会导致 UB 到 delete cars[1]
而不重复 static_cast。使Car
多态可能是最好的行动原因。【参考方案2】:
当你的 SportsCar 类继承自 Car 时,你可以添加方法就好了。当您将 SportsCar 存储在向量中时,它仍会保留所有 SportsCar 数据,但您无法访问它,因为它仅被视为 Car。如果要访问“turbo()”方法,则必须将对象“cars[1]”转换为 SportsCar。
SportsCar mySportsCar = (SportsCar)cars[1];
//Now you can call .turbo() on mySportsCar
//You could also do it in one line, like so :
((SportsCar)cars[1]).turbo();
您必须记住一件事,您的向量中的任何对象都不是 SportsCar,因此您必须在转换它们之前检查类型,否则您会抛出异常。有多种方法可以做到这一点,这是一种:
if(SportsCar *mySportsCar = dynamic_cast<SportsCar*>(&cars[0]))
//Your car was successfully casted, you can use it here
else
//Your car is not a SportsCar
【讨论】:
因使用 C 风格转换而被否决;如果您重写以使用 C++ 强制转换,将会更新。 (实际上在某些极端情况下,C++ 中需要 C 样式转换,但这不是其中之一)。此外,dynamic_cast
仅在基础具有虚函数时才有效。【参考方案3】:
cars
是vector<Car*>
,而Car
确实没有turbo()
成员函数。
C++ 使用静态名称查找。它不会查看Car*
,看到它在运行时指向Sportscar
,然后在Sportscar
中查找turbo
。
如果您想要这种动态行为,那么您必须自己编写它(在修复您的基类以实际支持多态之后):
if (auto sc = dynamic_cast<Sportscar*>(cars[1]))
sc->turbo();
else
std::cout << "cars[1] is not a Sportscar\n";
(当然,既然你知道这是一辆跑车,你也可以无条件地static_cast
它)
您可以获得的动态行为是针对虚拟方法:基类通过虚拟方法声明一些“接口”,然后您可以在该基类型的类上使用这些虚拟方法。在运行时,如果类确实是派生类型,则调用派生方法。
struct Base
virtual void foo() std::cout << "Base\n";
virtual ~Base()
;
struct Derived : Base
virtual void foo() override std::cout << "Derived\n";
;
int main()
Base *b = new Derived;
b->foo(); // prints "Derived"
只允许在b
上调用foo
,因为在编译时,编译器可以看到Base
有一个方法foo
,但由于该方法是虚拟的,因此编译器还设置了机制,以便在运行时调用的实际方法实现在运行时由对象的实际类型确定。但是为了设置它,编译器必须看到 foo
是一个虚函数,而对 Derived
一无所知。
【讨论】:
dynamic_cast
仅适用于多态类; Car
需要有一个虚函数(最好是析构函数)才能使该强制转换工作。
@MattMcNabb 是的,这是真的。我没有注意到它还没有。以上是关于将对象存储在向量中,编译器说元素不存在的主要内容,如果未能解决你的问题,请参考以下文章