-2

I have a problem regarding inheritance on C++. The Vehicle class has SetStartingPosition() and GetStartingPosition() functions. The Car class inherits these functions and sets the starting position to 5 in the constructor with SetStartingPosition(5).

What I want is that when a new vehicle (new Car) is added to a lane (in the Lane class) with the use of the AddVehicleToALane(Vehicle* vehicle) function, the position of this Car is assigned the value of 5.

Vehicle.h:

Class Vehicle{
public:
    Vehicle();
    Vehicle(double position);
    virtual ~Vehicle();
    void SetStartingPosition(double startingpos){
        fStartingPos = startingpos
    }

    double GetStartingPosition(){
        return fStartingPos;
    }

private:
    double fStartingPos;
}    

Car.h:

#include "vehicle.h"
Class Car: public Vehicle{
public:
    Car();
    Car(double position);
}

Car.cpp:

Car::Car(double position): Vehicle(position){
    SetStartingPosition(5);
}

Lane.h:

#include "vehicle.h"
#include "car.h"
Class Lane{
public:
    void AddRandomVehicle();

    void AddVehicleToALane(Vehicle* vehicle){
     fVehicles.push_back(vehicle);
    }

private:
    std::vector<Vehicle*> fVehicles;
    Vehicle *v;
}

Lane.cpp:

void Lane::AddRandomVehicle(){
    int i = 0;
    AddVehicleToALane(new Car(fVehicles[i]->GetStartingPosition()));
}

The final command v->GetStartingPosition() doesn't work. Basically, when a new instance of Car (new Car) is created, I want this car to obtain the starting position = 5 (from car.cpp), but the final command doesn't work.

Could anyone give me some tips as how to solve this? I don't think it should be too difficult, but I've spent hours trying to figure out and I can't solve it! Any help would be greatly appreciated.

L. Howard
  • 19
  • 2
  • 7
    You use the phrase "_doesn't work_" multiple times, but fail to explain _how_ it doesn't work. – Algirdas Preidžius Jan 02 '18 at 12:33
  • Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Jan 02 '18 at 12:34
  • What about `AddVehicleToALane(new Car(0));`? Your code passes a position to the constructor and ignores it, so you can pass anything. Not an answer because I could not understand what you are trying to achieve here... – Serge Ballesta Jan 02 '18 at 12:39
  • Should using fVehicles[int i]->GetStartingPosition() work? I tried running it, but it crashes every time I run it, but I'm not sure why. – L. Howard Jan 02 '18 at 12:40
  • 1
    `v` doesn't point to a `Vehicle` (or anything, really), so you cannot call *any* functions using that pointer. – Bo Persson Jan 02 '18 at 12:41
  • `Class Car`you don't show real code! –  Jan 02 '18 at 12:41
  • This line: Car::Car(double position) : Vehicle(position) ... How do you call the Vehicle constructor from the Car constructor and pass it "position"? When Vehicle has no such constructor taking the argument? – Zebrafish Jan 02 '18 at 12:44
  • @AlgirdasPreidžius, when i replace v->GetStartingPosition() with 5, the programme runs and the number 5 is stored as the starting position, but i don't know how to inherit the starting position from the car class. – L. Howard Jan 02 '18 at 12:44
  • @Zebrafish what do you mean by that? – L. Howard Jan 02 '18 at 12:46
  • @BoPersson I will edit the code to show what I think another solution could be, replacing v->GetStartingPosition() with fVehicles[i]->GetStartingPosition(). Should this work? – L. Howard Jan 02 '18 at 12:47
  • @L.Howard - You will have a similar problem with `fVehicle`. It is initially empty, so when you create the first car there is nothing to read from. – Bo Persson Jan 02 '18 at 12:51
  • @BoPersson do you have any idea how I can solve that? Can i just initialise i = 0, then use fVehicles[i]->GetStartingPosition()? – L. Howard Jan 02 '18 at 12:55
  • @L.Howard The only thing I could suggest is learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). There are plenty of things wrong with the code you shared, in addition to, your comments suggesting that you are mixing up _instances_ of the class, with the _class_ itself. – Algirdas Preidžius Jan 02 '18 at 12:55
  • @AlgirdasPreidžius i want to set the position (equal to 5) of an instance of Car (new Car) instantiated in the Lane.cpp file, inheriting this value of 5 from the Car class. Do you know how i could do that? – L. Howard Jan 02 '18 at 13:18

1 Answers1

3

There are many things wrong with your code.

Lane::v is never assigned, so when you v->GetStartingPosition(), you have undefined behaviour.

Vehicle doesn't have a constructor that accepts a double, so Car::Car(double position): Vehicle(position) is a syntax error.

Car::Car(double position) is private, so you can only new Car(...) from within Car methods

Why does car take a double position and then overwrite it? why not Car::Car() { SetStartingPosition(5); }?

Why do you have a Car class at all? You don't have anything to distinguish it from Vehicle after it is constructed, and none of the methods of Vehicle are virtual (particularly the destructor!)

Caleth
  • 52,200
  • 2
  • 44
  • 75
  • 2
    Also, *Car(double position)* is private so he won't be able to create a Car at all. – dsp_user Jan 02 '18 at 12:58
  • These are helpful tips. @Caleth are you saying that my SetStartingPosition(5) function can be placed in the Car::Car() {} constructor? – L. Howard Jan 02 '18 at 13:07
  • @dsp_user could you perhaps explain how i could fix that? – L. Howard Jan 02 '18 at 13:08
  • 1
    just add public: Car(); Car(double); A class has, unlike a struct, all members private unless explicitly declared public (or protected) – dsp_user Jan 02 '18 at 13:10
  • @dsp_user and Caleth the main problem i have is that I want to be able to extract the value of 5 from the SetStartingPosition(5) function in the Car.cpp class and put this value of 5 as the position of the instance of the Car "new Car" in the lane.cpp file. Do you understand my problem? – L. Howard Jan 02 '18 at 13:14
  • does `AddVehicleToALane(new Car(5))` not do what you want? – Caleth Jan 02 '18 at 13:17
  • 1
    Note that you still need to define the body for your constructor (*Vehicle(double position) {} * ( in this case you defined an empty body ( {} ), which doesn't make sense because you passed an argument to the constructor so it's better to use something like *Vehicle(double position) { fStartingPos = position; }* or even *Vehicle(double position) : fStartingPos(position) {} * – dsp_user Jan 02 '18 at 13:41
  • @Caleth you're quite right that does what i want it to do, but doing AddVehicleToALane(new Car(5)) is not inheriting the Starting Position from Car.cpp. Do you see what I mean? – L. Howard Jan 02 '18 at 14:17
  • @Caleth i want to obtain the value of 5 from the function SetStartingPosition(5) in car.cpp. – L. Howard Jan 02 '18 at 14:19
  • @Caleth your explanation worked!!! my SetStartingPosition() function was in the wrong constructor!!!!!!! – L. Howard Jan 02 '18 at 14:33
  • Thank you all for the help, I appreciate it greatly! Keep helping others, it's great. – L. Howard Jan 02 '18 at 14:34