Using VS2012, I have encountered an unprecedented problem, my main class have a member, which is a an array of another small class. This small class has an explicit constructor. Now the problem: How to supply the initialization list?
The following is the code:
class A { //This is the small class
private:
int value;
public:
explicit A(int newNumber) {
value = newNumber;
}
};
class B {//This is the major class
private:
A arrayOfA[5];
public:
B() {//Compiler complain "no default constructor for class A"
//But I really don't know how to supply the initialization list
}
};
Thanks a lot!
With a constructor initializer list:
B() : arrayOfA{A(1), A(2), A(3), A(4), A(5)} {}
I would also suggest changing the array to a std::array
. If this is not C++11, I'm afraid you cannot initialize your member without having a default constructor for A
. However, there is still the possibility of making your own (it's not hard) or using boost::array
. In such a case, I am not sure whether it is possible to initialize the array directly, but it definitely is with a helper function.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments