Hi experts,
Why does line parts = new Tire*[numOfParts]; generate the error invalid conversion from `Tire**' to `CarPart**' in file car.cpp? The program works if I change the variable parts to be of type Tire** instead of CarPart**.
I formatted my problem to a complete simple (though multifile) program, to make it readable and possibly useful for others having similar problems. The idea is to have a tree like structure that represents a car. A part of the car, say a tire, is connected to the car thus must get some information of the car. So, I'm passing a pointer of the car to the tire. I got that working after some trouble. But because all the parts of the car will have similar functions, I have an abstract base class CarPart. Now I'm trying to assign a pointer to Tire to a variable of pointer to CarPart. This is suppose to be whole idea of abstract base class, so why the error?
Files of the program:
carPart.h
#ifndef carPart_H
#define carPart_H
#include "vectors.h"
using namespace std;
class CarPart { //CAR PART (ABSTRACT BASE CLASS)
public:
CarPart* parent; //Pointer to the parent of this part
Vector3f vel; //Velocity vector in the world
void addParent(CarPart* parent) {this->parent = parent;}
virtual void setVel(Vector3f vel) = 0; //A pure virtual function
};
#endif
tire.h
#ifndef tire_H
#define tire_H
#include "vectors.h"
#include "carPart.h"
#include "car.h"
using namespace std;
class Tire : public CarPart{
public:
//These are inherited from the base class: Adding indentical redefinition here
//wouldn't stop from compiling, but has proven to cause problems, when trying
//access these member via pointer to instance
/*
CarPart* parent; //Pointer to the parent of this part
Vector3f vel; //Velocity vector in the world
void addParent(CarPart* parent) {this->parent = parent;} */
//Adding possibility to have parts:
CarPart** parts;
int numOfParts;
void setVel(Vector3f vel); //Adding definition for function setVel
Tire(); //Constructor
};
#endif
tire.cpp
#include "vectors.h"
#include "carPart.h"
#include "car.h"
#include "tire.h"
using namespace std;
void Tire::setVel(Vector3f vel) //Adding definition for function setVel
{
this->vel = vel;
}
Tire::Tire() //Constructor
{
vel = Vector3f(0.0f,0.0f,0.0f);
}
car.h
#ifndef car_H
#define car_H
#include "vectors.h"
#include "carPart.h"
#include "tire.h"
using namespace std;
class Car : public CarPart{
public:
//These are inherited from the base class: Adding indentical redefinition here
//wouldn't stop from compiling, but has proven to cause problems, when trying
//access these member via pointer to instance
/*
CarPart* parent; //Pointer to the parent of this part
Vector3f vel; //Velocity vector in the world
void addParent(CarPart* parent) {this->parent = parent;} */
//Adding possibility to have parts:
CarPart** parts;
Tire** tires;
int numOfParts;
void setVel(Vector3f vel); //Adding definition for function setVel
Car(); //Constructor
};
#endif
car.cpp
#include "vectors.h"
#include "carPart.h"
#include "car.h"
#include "tire.h"
using namespace std;
//THIS WORKS
Car::Car()
{
vel = Vector3f(5.0f,5.0f,5.0f);
numOfParts = 4;
tires = new Tire*[numOfParts];
for (int i = 0; i < numOfParts; i++)
{
tires[i] = new Tire();
tires[i]->addParent(this);
}
tires[0]->vel = Vector3f(1.0f,1.0f,1.0f);
tires[1]->vel = Vector3f(2.0f,1.0f,1.0f);
tires[2]->vel = Vector3f(3.0f,1.0f,1.0f);
tires[3]->vel = Vector3f(4.0f,1.0f,1.0f);
}
/*
Car::Car() //DOESN'T WORK, WHY??
{
vel = Vector3f(5.0f,5.0f,5.0f);
numOfParts = 4;
parts = new Tire*[numOfParts]; //ERROR MESSAGE: invalid conversion from `Tire**' to `CarPart**'
for (int i = 0; i < numOfParts; i++)
{
parts[i] = new Tire();
parts[i]->addParent(this);
}
parts[0]->vel = Vector3f(1.0f,1.0f,1.0f);
parts[1]->vel = Vector3f(2.0f,1.0f,1.0f);
parts[2]->vel = Vector3f(3.0f,1.0f,1.0f);
parts[3]->vel = Vector3f(4.0f,1.0f,1.0f);
}
*/
void Car::setVel(Vector3f vel) //Adding definition for function setVel
{
this->vel = vel;
}
main.cpp
#include <iostream>
#include "vectors.h"
#include "car.h"
#include "carPart.h"
#include "tire.h"
using namespace std;
int main (int argc, char *argv[])
{
Car myCar;
Tire newTire;
char quit;
quit = '\0';
while (quit != 'q')
{
cout << "Hello ! Testing set of Car classes." << endl;
cout << "" << endl;
cout << "Press q and enter to quit." << endl;
cout << "\n";
cout << "\nThe car seems to have access to it's parts:";
cout << "\n";
cout << "\nmyCar.tires[0]->vel.x: " << myCar.tires[0]->vel.x;
cout << "\nmyCar.tires[0]->vel.y: " << myCar.tires[0]->vel.y;
cout << "\nmyCar.tires[0]->vel.z: " << myCar.tires[0]->vel.z;
cout << "\n";
cout << "\nmyCar.tires[1]->vel.x: " << myCar.tires[1]->vel.x;
cout << "\nmyCar.tires[1]->vel.y: " << myCar.tires[1]->vel.y;
cout << "\nmyCar.tires[1]->vel.z: " << myCar.tires[1]->vel.z;
cout << "\n";
cout << "\nAnd great, the tire also finds it's parent:";
cout << "\n";
cout << "\nmyCar.tires[0]->parent->vel.x: " << myCar.tires[0]->parent->vel.x;
cout << "\nmyCar.tires[0]->parent->vel.y: " << myCar.tires[0]->parent->vel.y;
cout << "\nmyCar.tires[0]->parent->vel.z: " << myCar.tires[0]->parent->vel.z;
cout << "\n";
cout << "\nmyCar.tires[1]->parent->vel.x: " << myCar.tires[1]->parent->vel.x;
cout << "\nmyCar.tires[1]->parent->vel.y: " << myCar.tires[1]->parent->vel.y;
cout << "\nmyCar.tires[1]->parent->vel.z: " << myCar.tires[1]->parent->vel.z;
cout << "\n";
cin >> quit;
}
return 0;
}