I have a class obj1 that has no default constructor, and class obj2 that also doesn't have a default constructor, and has as private variable an element of obj1:
I would like something like the following code - but actually this doesn't compile, telling me that obj1 has no default constructor.
class obj1{
obj1(some parameters){};
}
class obj2{
obj1 _myObj1;
obj2(some parameters){
_myObj1 = obj1(some parameters)
}
}
any ideas?
You need to make the constructor accessable to class obj2. This can be accomplished by making it public so that all other classes can use it or you can mark obj2 as a friend of obj1.
or mark the constructor as public
You must put your constructor in
public
area:and even you second class:
More:
In fact, private constructors are useful when you want forbid your code to instance an object directly. The most popular usage of private constructors are Singleton classes.
Make the constructor of
obj1
public and use initialization list inobj2
.You need to make your constructors public and you need to call the obj1 in the initialization list of obj2 constructor.