?
Solved

C2107: illegal index, indirection not allowed error when compiling

Posted on 2015-01-19
14
Medium Priority
?
774 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

765 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