I have a class, and in this class I’d like to have an array of another class, declares, any suggestions on how to do this?
But when I do the following, I get an error.
#include "classB.h"
Class classA
{
public:
classA();
int intArray[20];
classB arrayOfClassB[20];
};
I get:
error C2512: ‘classB’ : no appropriate default constructor available.
My classB constructor takes a string.
When you define an array in this way
classB arrayOfClassB[20];, each element ofarrayOfClassBwill be constructed using the default constructor ofclassB. Since you have defined a constructor inclassBwhich takes a string as a parameter, the compiler will NOT generate a default constructor of classB for you. In this case, you have to define a default constructorclassB()inclassB, then the error is gone. However, as others said, using vector is a better choice.