Search code examples
c++classborland-c++

Declaring an array of class type (having problems with Borland C++)


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.


Solution

  • 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. ]