Hi,
I was hoping someone could correct my mistake.
I'm trying to store references (or pointers should that be better) in a hetrogenous List.
(The references or pointers will point to mixed classes).
The problem is that everytime I store the pointer which holds a reference to the object, the non base class datamembers are being lost.
I have done some research, but am just not getting it, so if someone could tell me how to correct it I would be most appreciated.
I've spent ages on this and am getting nowhere, and don't have broadband at home. I've read my book though.
Here are the two classes:
class Vehicle
{
public:
Vehicle(int publicmpg = 0);
protected:
int privatempg;
};
Vehicle::Vehicle(int publicmpg)
{
privatempg = publicmpg;
}
//---------------------------------
class SportsCar : public Vehicle
{
public:
SportsCar(int publicsixspeed = 0, int publicmpg = 0);
protected:
int protectedsixspeed;
};
SportsCar::SportsCar(int publicsixspeed, int publicmpg) : Vehicle(publicmpg)
{
protectedsixspeed = publicsixspeed;
}
and here is main.cpp:
#include <iostream>
#include <list>
#include "Vehicle.h"
using namespace std;
int main()
{
list<Vehicle> listname;
int publicmpg = 0;
int publicsixspeed = 0;
int publicelectricroof = 0;
SportsCar node(publicmpg, publicsixspeed);
SportsCar *ptrToSportsCar;
ptrToSportsCar = &node;
listname.push_back( *ptrToSportsCar );
system ("PAUSE");
return 1;
}
I know I should change:
list<Vehicle> listname;
to:
list<Vehicle*> listname;
And when I do Visual Studio 2005 reports:
error C2664: 'std::list<_Ty>::push_back' : cannot convert parameter 1 from 'SportsCar' to 'Vehicle *const &'
with
[
_Ty=Vehicle *
]
Reason: cannot convert from 'SportsCar' to 'Vehicle *const '
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Could anyone show me what else I need to do to get this working?
many thanks for any help1
greeny :)