Question

I have a class, let's say

class XXX{
...
};

i want to declare a global array of objects of that class, for example

XXX* arr = new XXX[50];

but, for example, i declare in xxx.h file (class' header file):

extern XXX* arr;

and in xxx.cpp file i do:

XXX* arr = new XXX[50];

But, Borland C++ gives the following errors:

Cannot find default constructor to initialize array element of type 'XXX'

When i just declare the following in xxx.cpp file

XXX* arr[50];

i get some error messages in other cpp files like

Undefined symbol arr;

So, to sum it up, how do i declare this array of mine?

UPDATE: I have a constructor with 3 arguments, and i don't want them to have default values.

Also, i "declared" the constructor inside a macro which goes like this:

#define PREP(num, c)\
    IVT ivt##num(evInt##num,num,c);\
    void interrupt evInt##num(...){\
    iv[##num]->signalize();\
 } 

So, IVT ivt##num(evInt##num,num,c); is the constructor of the class IVT, and i want to declare a global array of pointers with X elements.

Was it helpful?

Solution 2

Cannot find default constructor to initialize array element of type 'XXX'

you need default constructor because you are going to use it to create default instances of your class in this staement:

XXX* arr = new XXX[50];

probably you have implemented another constructors, some with arguments and this is why a default constructor was not auto generated by the compiler for you. It might looks like this:

class XXX{
public:
    XXX(){}
};

Next:

When i just declare the following in xxx.cpp file

XXX* arr[50];

i get some error messages in other cpp files like

Undefined symbol arr;

you need an extern declaration still in header to resolve the symbol in other translation units. This is what C++03 standard says what they are:

The text of the program is kept in units called source files in this International Standard. A source file together with all the headers (17.4.1.2) and source files included (16.2) via the preprocessing directive #include, less any source lines skipped by any of the conditional inclusion (16.1) preprocessing direc- tives, is called a translation unit. [Note: a C++ program need not all be translated at the same time. ]

OTHER TIPS

Cannot find default constructor to initialize array element of type 'XXX'

Means, you don't have a constructor that doesn't take any arguments (i.e., default constructor). Did you define it in your XXX class ? If you have constructor taking any arguments, then compiler doesn't provide the default constructor.

XXX* arr = new XXX[50];

The above statement invokes default constructor 50 times. BTW, please give a meaningful class name. It's a bit offensive.

When i just declare the following in xxx.cpp file

If you declare in the .cpp file alone, then the variable is visible to current translation unit alone. You still need to have extern declaration in the header file for other source object files to be able to link to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top