I’m trying to pass a variable by reference to a different class, but I cannot get it to work.
I have some config object that I create in the main application, then when I run the config dialog, I want to provide this config object to the dialog.
This is what I have so far:
ConfigDialog.h:
class ConfigDialog {
public:
explicit ConfigDialog(kimai::Config& config, QWidget *parent = 0);
private:
kimai::Config& config_;
};
ConfigDialog.cpp:
ConfigDialog::ConfigDialog(kimai::Config& config, QWidget *parent) {
config_ = config;
// Do something with config_ - get/set values, etc.
}
When I try to compile, I get the following error:
ConfigDialog.cpp:7: error: C2758: 'ConfigDialog::config_' : must be initialized in constructor base/member initializer list
Any idea how to fix this issue?
(I tried adding config_ = config to the initialization list but this is not valid)
You cannot initialize a reference by assignment, it needs to be done in the initializer list:
The
config_ = configassignment is a call of the assignment operator on the value being referenced byconfiginto the variable being referenced byconfig_, which is uninitialized. Assignment syntax works only when it is combined with the declaration: