Link to home
Start Free TrialLog in
Avatar of Tim Titus
Tim TitusFlag for United States of America

asked on

C2107: illegal index, indirection not allowed error when compiling

I have a basic structure defined in my header file:

struct MapMasterStruct
{
     char MapName[1024];
     char MapFile[1024];
     DWORD MapDataCount;
};

I have a global definition:
DWORD MapMasterIndex=0;
struct MapMasterStruct MapMaster[100];

When I have code that references this, I get an error:

MapMaster[MapMasterIndex].MapDataCount=12;

error 2107: illegal index, indirection not allowed
error 2228: left of '.MapDataCount' must have class/struct/union type

This is a .cpp file.

I have no idea what would cause this.
Avatar of ozo
ozo
Flag of United States of America image

It works for me.  Could the error be in another part of the program?
Avatar of phoffric
phoffric

I didn't see a problem, so I copied your code. Your code builds and runs ok in VS 2010 express. What are you not telling us?
struct MapMasterStruct
 {
      char MapName[1024];
      char MapFile[1024];
      int MapDataCount;
 };

 int MapMasterIndex=0;
 struct MapMasterStruct MapMaster[100];

 int main() {
    MapMaster[MapMasterIndex].MapDataCount=12;
 }

Open in new window

Yes, more context around

MapMaster[MapMasterIndex].MapDataCount=12;

Open in new window

would be helpful. Just as a test, what message does

(MapMaster + MapMasterIndex)->MapDataCount=12;

Open in new window


generate?
Post in code block the smallest program that shows the problem.
Avatar of Tim Titus

ASKER

Sorry folks, this is a weird one for me.  Each time I compile my 15000line program, the error seems to move to different lines.

I have also compiled the above test and it works fine (proving that I asked the wrong question!)

Let me try to narrow it down a tad.

When I compile:
sprintf(Filename, "%s\\%s", InstallDir, (MapMaster + MapMasterIndex).MapFile);
It results in: error C2228: left of '.MapFile' must have class/struct/union type

When I compile:
sprintf(Filename, "%s\\"%s", InstallDir, MapMaster[MapMasterIndex].MapFile);
It works.

The problem is that it did not work just 30minutes ago, and I swear nothing changed on this line, or the header file.

I still have over 100 errors like this throughout the code.

What was changed is as follows:
MapFile used to be a global variable.  It was changed to be included in the MapMaster global variable so there could be multiple instances of the MapFile (and associated variables).

Any ideas why changing a global variable to an indexed global variable might cause this error?
Given that you refactored by moving MapFile from a global to a struct data member, I would search for MapFile in your project and see if there is a line of code that you left in, say, possibly an extern in one of your header files.
Ok, another clue:

If I change the following code:
MapMaster[MapMasterIndex].MapData

Open in new window

to
MapMaster[0].MapData

Open in new window

It compiles successfully and goes to the next line.
A search for MapFile shows that that variable is now only defined inside the struct of the header file.  Its definition does not exist inside any other files.
What is the value of MapMasterIndex?
You know that it cannot be 100 or greater. The index is allowed to be in the range 0..99 for an array of 100 elements.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The value of MapMasterIndex is not defined yet, as I can't get the program to compile.  It is a range of 0 to 99 though.
If you were to try to create a separate small program that illustrates the problem by copy/paste your 1500 line program, then one of two things may happen - you post it and we fix it; or as you are doing this effort, you see something and you fix it yourself.
Ok, the "another context" got me to thinking about this the right way.  I had a function variable defined as DWORD *MapMasterIndex and it should have been DWORD MapMasterIndex.

When I used MapMaster[MapMasterIndex].MapFile it blew chunks because it tried to use the address for MapMasterIndex and not the actual numeric offset index.

Thanks!!!
You mention many other errors. Sometimes, fixing the first few different errors at the top of the list, will cause some cascading errors to disappear. Try fixing as many errors as you can to get to a small set.