Solved

C2107: illegal index, indirection not allowed error when compiling

Posted on 2015-01-19
14
414 Views
Last Modified: 2015-01-19
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.
0
Comment
Question by:ttitus
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40558853
It works for me.  Could the error be in another part of the program?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 40558863
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

0
 
LVL 86

Expert Comment

by:jkr
ID: 40558868
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?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 40558871
Post in code block the smallest program that shows the problem.
0
 

Author Comment

by:ttitus
ID: 40558881
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?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 40558888
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.
0
 

Author Comment

by:ttitus
ID: 40558890
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.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:ttitus
ID: 40558894
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.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 40558897
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.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40558902
Is ther a chance that the symbol 'MapMaster' is used in another context?
0
 

Author Comment

by:ttitus
ID: 40558905
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.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 40558910
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.
0
 

Author Closing Comment

by:ttitus
ID: 40558914
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!!!
0
 
LVL 32

Expert Comment

by:phoffric
ID: 40558918
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.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now