c++arraysclassstructinclude

Declareing array that stores a struct, which defined in another file included, gives the error "Incomplete Type "Array[]" is not allowed"


I have defined a struct in another file called file1.h (example) and when I declare an array using that struct in another file called File2.h, i get the error Message:

"Incomplete Type "array[]" is not allowed"

Example Code:

File1.h:

#ifndef MYSTRUCT_H
#define MYSTRUCT_H
    
struct MyStruct 
{
    float number1;
    float number2;
};
#endif

File2.h:

#ifndef MY_CLASS_H
#define MY_CLASS_H
#include "File1.h"

class MyClass 
{
public:
    MyStruct array[]; // I get the error here
};

#endif

Solution

  • you need to either assign the values while creating the array or specify the size of the array MyStruct array[10]

    The compiler needs to know how many elements you will have