Reputation: 1744
I got a bit of issue here.
this is my main cpp file
int main() {
// assuming max size is up to 100
VehicleTwoD *vehicletwod[100];
int vehicleCounter = 0;
Car *car = new Car();
Motorcycle *motor = new Motorcycle();
cout << " Please enter name of vehicle ";
cin >> vehicleName;
if (vehicleName == "Car") {
vehicletwod[vehicleCounter] = car;
//issue here
vehicletwod[vehicleCounter].setName(vehicleName);
//issue end
}
return 0;
}
This is my car.cpp
struct Point {
int x, y;
};
class Car: public Vehicle {
private:
Point wPoint[4];
double area;
double perimter;
public:
void setType(string);
void setPoint();
string getType();
string getPoint();
}
The issue here is setName is a function of Vehicle, but not a function of Car. but i did the inheritance which should inherit the function. but doesn't seems to work.
It say...Request for setName in vehicletwod[vehicleCounter]' which is of non class type VehicleTwoD*
Above Issue is fixed
Additional Issue:
Okay i fixed the previous issue by changing . to ->
Here the other issue.
as the code is. on this part
if (vehicleName == "Car") {
vehicletwod[vehicleCounter] = car;
vehicletwod[vehicleCounter]->setName(vehicleName);
//now i want to setPoint which is a function of child class
vehicletwod[vehicleCounter]->setPoint();
//issue here
}
I try to setPoint which is a function of child class, Car
However it say.. Vehicle has no member named 'setPoint'
After doing what John mention.. the above issue is also fixed..
But the hardest part is on how to retrieve what been set since it is a vehicletwod object and not a car object.
Assume after setPoint, i want to getPoint()
i try do this
vehicletwod[0]->getPoint();
I get an error saying getPoint is of non-class type 'vehicletwod'
Upvotes: 0
Views: 329
Reputation: 465
VehicleTwoD has not setPoint(), so you must dynamic_cast VehicleTwoD * to Car*, but you must make sure vehicletwod[vehicleCounter] really points to an object of Car, otherwise it's a runtime error!
dynamic_cast<Car*>(vehicletwod[vehicleCounter])->setPoint();
The best way is make setPoint() as a pure virtual function of VehicleTwoD;
class VehicleTwoD {
public:
virtual void setPoint() = 0;
/***/
};
implement the setPoint() in derived class.
Upvotes: 0
Reputation: 42
It seems lik VehicleTwoD
doesn't have a method like this:
protected:
virtual void setPoint();
You need this because of the definition of the array.
Upvotes: 0
Reputation: 605
vehicletwod[vehicleCounter] is a pointer so you need to dereference it. Try vehicletwod[vehicleCounter]->setName(vehicleName).
You might want to read up on pointers. Google "c++ class pointer basics" or checkout http://www.codeproject.com/Articles/627/A-Beginner-s-Guide-to-Pointers
Upvotes: 0
Reputation: 87959
OK still don't know what VehicleTwoD
is but you would be closer to correct if you wrote
vehicletwod[vehicleCounter]->setName(vehicleName);
instead of
vehicletwod[vehicleCounter].setName(vehicleName);
You have an array of pointers, so you need to use ->
.
The other problem is that setPoint
is only a member of Car
, not Vehicle
, so you have to call it on your car before you add it to your array.
Something like this
if (vehicleName == "Car") {
car->setPoint();
vehicletwod[vehicleCounter] = car;
vehicletwod[vehicleCounter]->setName(vehicleName);
}
Upvotes: 2
Reputation: 2894
You have to instantiate Car and Motorcycle differently:
VehicleTwoD *car = new Car();
VehicleTwoD *motor = new Motorcycle();
then declare the methods you want to use in child classes as protected.
You are welcome!
PD: A car is not VehicleTwoD, taxonomy matters.
Upvotes: 0