Passing an array of structs

Hi all,

I'm sure this is quite a simple one, but as a beginner i just can't work it out. Any help would be much appreciated.

I have a main form Form1, which includes classes in headers called staticData.h and dynamicData.h

Within staticData.h, i have a struct called city:

struct city

{

public: int cityID;

char cityName[22];

char countryID[2];

char countryName[20];

double lng, lat;

int annualDemand;

int populationrank;

bool shown;

};

When i initialise form 1, in form1.cpp, i declare a global array of city structs (City)

#include "stdafx.h"

#include "staticData.h"

#include "dynamicData.h"

#include "Form1.h"

using namespace supplychaingame;

city City[800];

[STAThreadAttribute]

int Main()

{

Application::EnableVisualStyles();

// Application::EnableRTLMirroring();

Application::Run(gcnew Form1());

return 0;

}


.... the City data is populated from a file when Form1 loads.

The City data is then used numerous times around the Form1, staticData classes, using the syntax:

extern
city City[];

to define the variable


This has been working just fine.

However, when I try and add this syntax in a managed class in my dynamicData header, I get an error on the staticData sheet -

'City' : city[] differs in levels of indirection from "unknown-type"

the errors match the extern city City[]; declarations in staticData

Any ideas where I'm going wrong here



Answer this question

Passing an array of structs

  • Ilyas Kazi

    The compiler is correct:

    city City[800]

    and

    extern city City[];

    are very different - the compiler cannot know how much memory is required in the second declaration. Try the following instead:

    extern city City[800];

    this will ensure that the compiler always knows how big the array is.

    Even better don't use global variables like this.



  • LaoZheng

    Thank-you for your reply.

    Changing to city City[800];

    doesn't make any difference, however. I still get the same error.

    I am actually gradually in the process of migrating away from all the global variables. However, in the meantime, I need to be able to get this to work.

    In a nutshell, how can I make my global struct array visible to all classes without getting a 'differs in levels of indirection' error
    Thanks

  • Passing an array of structs