7

I have made class in C++ and I wanted it to have a field Osoba& but I get an weird error:

class Rachunek{
public:
    Osoba& wlasciciel;
    double stan_konta;
    Rachunek(Osoba* wlasciciel, double stan_konta){ //Uninitialized reference member
        this->wlasciciel = wlasciciel;
        this->stan_konta = stan_konta;

    }
};
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • 3
    Do you understand what reference types are, and what pointer types are? – Kerrek SB May 19 '13 at 11:30
  • The error is self-explanatory and should be expected. You have a reference member but you haven't initialized it (i.e. bound it to an object at construction time) which is an error. – CB Bailey May 19 '13 at 11:33

2 Answers2

16

Use initializing list like this: (Best approach)

class Rachunek{
public:
    Osoba& wlasciciel;
    double stan_konta;
    Rachunek(Osoba* wlasciciel, double stan_konta): 
        wlasciciel(*wlasciciel) , 
        stan_konta(stan_konta)  { //Uninitialized reference member


    }
};

You have a reference as a member and a reference must be initialized right away. This notation allows for initialization at declaration time. If you instead used a normal member without & it would work fine as you did it. Though the presented style here is more efficient.

Alternativly: (Lesser efficient approach)

class Rachunek{
public:
    Osoba wlasciciel; // Note missing & on the type. 
    double stan_konta;
    Rachunek(Osoba* wlasciciel, double stan_konta)
    {
        this->wlasciciel = *wlasciciel;  
        this->stan_konta = stan_konta;  

    }
};
CodeTower
  • 6,293
  • 5
  • 30
  • 54
  • What it does? This is weird notation. Should I add `this->stan_konta = stan_konta` – Yoda May 19 '13 at 11:33
  • That would not be necessary. You can mix initalizer lists with old school style. Generally use the initializer list as it more effecient. – CodeTower May 19 '13 at 11:40
  • How is what the OP originally wrote different from what is written here? It seems that manually assigning the reference WITHIN the constructor, such as with `this->wlasciciel = wlasciciel;`, would also be initializing it at construction (declaration?) time. Isn't the point of constructors to initialize an object's member variables right away as an object is created? – LazerSharks Nov 27 '14 at 02:02
  • In a perfect world, perhaps that would be the case. However, according to C++ initialization order the last part of initialization is execution of the constructor (http://en.cppreference.com/w/cpp/language/initializer_list). Hence the member variables are initialized before execution of the constructor. Which in effect means in this case an uninitialized reference. That is why the initializer-list has been invented. – CodeTower Nov 27 '14 at 12:33
2

You need to use the constructor initialization list

Rachunek(Osoba* wlasciciel, double stan_konta)
      :wlasciciel (*wlasciciel)
      ,stan_konta (stan_konta)
{ 
}

It is obvious from your code that you lack a lot of basic C++ knowledge, which is fine, but please do refer to a good book

Community
  • 1
  • 1
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434