How can I use reference of a Class type which doesnt have a no argument default constructor as a member variable in another class ?
I am using references here like this.
x.h
Code:
class X{
public:
X(int v);
~X();
private:
int val;
};
x.cpp
Code:
#include "x.h"
X::X(int v): val(v){}
X::~X(){}
y.h
Code:
#include "x.h"
class X;
class Y{
public:
Y();
~Y();
private:
X& xi;
};
y.cpp
Code:
#include "y.h"
Y::Y():xi(2){}
Y::~Y(){}
The above yields compilation errors
Code:
error: invalid initialization of non-const reference of type ‘X&’ from a temporary of type ‘int’
If I use
Compiler Fires
Code:
error: invalid initialization of non-const reference of type ‘X&’ from a temporary of type ‘X’