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

idjuradj

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.

4pie0

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Declaring an array of pointers in C++

分類Dev

Declaring an array of type String containing numbers with padding

分類Dev

C++ declaring a static object in a class

分類Dev

Declaring variable inside functions of a class in C++

分類Dev

Having problems working with SIGINT signal in C / UNIX

分類Dev

Pointer to class data member having inherited type

分類Dev

Structure having an array of pointers in c

分類Dev

Will the future C++ standard support "Declaring type template parameters with auto"

分類Dev

Are data type identifiers necessary when declaring a function in C?

分類Dev

C++ declaring static enum vs enum in a class

分類Dev

Is declaring a class in C++ as "final : public virtual" EVER useful?

分類Dev

Declaring the type of 'this' in a typescript function?

分類Dev

Declaring static class variables

分類Dev

Having problems with debugging in .vscode

分類Dev

Having problems with precision in python

分類Dev

Problems Deserializing Nested JSON Array in C#

分類Dev

declaring pointer to 1-D Array inside 2-D Array in C++

分類Dev

Declaring C# properties

分類Dev

I am having Problems with Arrays

分類Dev

IntelliJ Having problems running Gradle

分類Dev

Having problems to implement a setOnClickListener - Fragment

分類Dev

C++ - Pointer to class type

分類Dev

Is "enum class" a class type in C++?

分類Dev

Arduino declaring a unsigned byte array

分類Dev

Declaring a dynamic array not working as expected

分類Dev

Borland C ++コンパイラのIostream

分類Dev

Java static class import in the declaring class

分類Dev

Swift Generic Protocol Class Type in Array

分類Dev

The Standard seems to support (the snippet below compiles) a static data member having the same type as the class itself

Related 関連記事

  1. 1

    Declaring an array of pointers in C++

  2. 2

    Declaring an array of type String containing numbers with padding

  3. 3

    C++ declaring a static object in a class

  4. 4

    Declaring variable inside functions of a class in C++

  5. 5

    Having problems working with SIGINT signal in C / UNIX

  6. 6

    Pointer to class data member having inherited type

  7. 7

    Structure having an array of pointers in c

  8. 8

    Will the future C++ standard support "Declaring type template parameters with auto"

  9. 9

    Are data type identifiers necessary when declaring a function in C?

  10. 10

    C++ declaring static enum vs enum in a class

  11. 11

    Is declaring a class in C++ as "final : public virtual" EVER useful?

  12. 12

    Declaring the type of 'this' in a typescript function?

  13. 13

    Declaring static class variables

  14. 14

    Having problems with debugging in .vscode

  15. 15

    Having problems with precision in python

  16. 16

    Problems Deserializing Nested JSON Array in C#

  17. 17

    declaring pointer to 1-D Array inside 2-D Array in C++

  18. 18

    Declaring C# properties

  19. 19

    I am having Problems with Arrays

  20. 20

    IntelliJ Having problems running Gradle

  21. 21

    Having problems to implement a setOnClickListener - Fragment

  22. 22

    C++ - Pointer to class type

  23. 23

    Is "enum class" a class type in C++?

  24. 24

    Arduino declaring a unsigned byte array

  25. 25

    Declaring a dynamic array not working as expected

  26. 26

    Borland C ++コンパイラのIostream

  27. 27

    Java static class import in the declaring class

  28. 28

    Swift Generic Protocol Class Type in Array

  29. 29

    The Standard seems to support (the snippet below compiles) a static data member having the same type as the class itself

ホットタグ

アーカイブ