-3

I get a negative output to a positive double variable. My object is: Fahrzeug fr("BMW",200.45);

 class Fahrzeug {
    private:
    string p_sName;
        double p_dMaxGeschwindigkeit;
    public:
        Fahrzeug(string p_sName, double p_dMaxgeschwindigkeit) {
            this->p_sName = p_sName; 
            this->p_dMaxGeschwindigkeit = p_dMaxGeschwindigkeit; //the output is negative
            cout << "Der Name des Fahrzeugs: " << this->p_sName << "\n" << "Die maximale Geschwindigkeit des Fahrzeugs: " << this->p_dMaxGeschwindigkeit << endl;
            }
    };
        #endif /* FAHRZEUG_H_*/
  • This is a typo. Although it wont help detect this type of problem, prefer a initializer list: [https://stackoverflow.com/questions/7665021/c-member-initialization-list](https://stackoverflow.com/questions/7665021/c-member-initialization-list) – drescherjm Nov 27 '21 at 22:42
  • C++ is case sensitive, and you have a typo based on case of names. So you're initialising an uninitialised variable with itsself - which causes undefined behaviour. – Peter Nov 27 '21 at 22:42

1 Answers1

1

this->p_dMaxGeschwindigkeit = p_dMaxGeschwindigkeit; this assigns the (uninitialised) member variable to itself.

You also have a parameter called p_dMaxgeschwindigkeit which you probably meant to use but note that its not the same spelling - it has a lowercase G and C++ is case sensitive.

Mike Vine
  • 9,468
  • 25
  • 44