Skip to main content
gamedev.net
🔒 Locked

Illegal Indirection

Started by cannyshammy 5 replies 4.1k views
Original Post
cannyshammy
cannyshammy
Hi Guys, Im trying to use Microsoft's Sample UI that comes with the directx 9c SDK but having a little trouble with the combo box control. When i create a combo box i do this for example:


int a = 1;

m_Interface.AddComboBox( MZE_VIEWCOMBO, 0, 0, 150, 32 );
m_Interface.GetComboBox( MZE_VIEWCOMBO )->AddItem( L"Maze View", &a );


thats all fine and dandy but when i try to get the data back:


void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl )
{

    DXUTComboBoxItem *pItem	= NULL

    int z;

    switch( nControlID )
    {
        case MZE_VIEWCOMBO:	
       
             pItem = ((CDXUTComboBox*)pControl)->GetSelectedItem();
           
             z = (int)*pItem->pData;

        break;
     
     }
}


I get an "C2100: illegal indirection" Error... Any ideas? Cheers all! Mike
Saandman
Saandman
I'm not familiar with that what you are using but here's the error in detail, basically you're dealing with non-pointers:

Visual C++ Concepts: Building a C/C++ Program

Compiler Error C2100
illegal indirection

Indirection operator ( * ) is applied to a nonpointer value. The following sample generates C2100:

// C2100.cppint main() {   int r = 0, *s = 0;   *r = 200;   // C2100   // try    // *s = 200;}


Edit:
z = (int)*pItem->pData;
I guess try this:
z = (int)*(pItem->pData);
________pro.gram.mer - an organism that turns caffeine into code
cannyshammy
cannyshammy
Nope tried that still the same error mate :( but cheers for replying so fast!

pData is of type void* btw if that helps?

mike
Muhammad Haggag
Muhammad Haggag
Quote:
Original post by cannyshammy
Nope tried that still the same error mate :( but cheers for replying so fast!

pData is of type void* btw if that helps?

mike

You can't dereference the void pointer as int value directly (because dereferencing the void pointer has no meaning - what should the value produced be?), as you do in the code. Do it like:
z = *static_cast<int*>(pItem->pData);

Which is simply casting the pointer to an int pointer, then dereferencing that.

cannyshammy
cannyshammy
Thanks coder, it now gets past the compile but isnt giving me the value that i originally put in as the data componant rather it is giving me large numbers such as 1981852.. I suppose something is going pearshaped a little still :P

Perhaps if i try to explain what i am doing a little more it may help..

What i am trying to do is have a combobox that allows the user to change the camera views based on thier selection, easy i thought as i had done this on my last project.. All i have to do is make a combo box and add items with data componants that can be accessed when an item is selected:

#define MZE_VIEWCOMBO 0#define MAZEVIEW 0#define PLAYERVIEW 1#define FPVIEW 2m_Interface.AddComboBox( MZE_VIEWCOMBO, 0, 0, 150, 32 );m_Interface.GetComboBox( MZE_VIEWCOMBO )->AddItem( L"Maze View", MAZEVIEW );m_Interface.GetComboBox( MZE_VIEWCOMBO )->AddItem( L"Player View", PLAYERVIEW );m_Interface.GetComboBox( MZE_VIEWCOMBO )->AddItem( L"First Person View", FPVIEW  );


However this gave me compile errors as it wanted a void pointer, which made sense because there is no way the creators could know what kind of data was going to go in there. But it was also confusing because on my other project when i did this:

m_Interface.AddComboBox( UI_OTHERCOMBO, 0, 0, 150, 32 );m_Interface.GetComboBox( UI_OTHERCOMBO )->AddItem( L"Something", TEXT("Some Text") );


it worked fine and i could get to the data fine, unless the TEXT() macro returns a pointer?

also i have noticed when i do:

m_Interface.AddComboBox( MZE_VIEWCOMBO, 0, 0, 150, 32 );m_Interface.GetComboBox( MZE_VIEWCOMBO )->AddItem( L"Maze View", false);// orm_Interface.AddComboBox( MZE_VIEWCOMBO, 0, 0, 150, 32 );m_Interface.GetComboBox( MZE_VIEWCOMBO )->AddItem( L"Maze View", 0);


then it works fine too?

So as you can see im trying to do a simple thing, im sure the creators must have seen this, perhaps ill go work through the SDK samples and see if there is any examples there!

Mike
Muhammad Haggag
Muhammad Haggag
Ah, sorry - I've not read how you passed the value to the combo box item. Basically, you passed the address of a local variable to the combo box. The problem with this is as soon as you exit that local scope (the code block in which the variable a is defined) the variable a is destroyed, and the address held in the combo box item points to garbage.

To explain some things first:
m_Interface.GetComboBox( UI_OTHERCOMBO )->AddItem( L"Something", TEXT("Some Text") );
The reason this works is because the TEXT() macro produces a string, which is a pointer to a character (or a wide character) - in short, a pointer. Pointers can be implicitly converted to void pointers, and that's what the compiler was doing.

m_Interface.GetComboBox( MZE_VIEWCOMBO )->AddItem( L"Maze View", false);
m_Interface.GetComboBox( MZE_VIEWCOMBO )->AddItem( L"Maze View", 0);


Both of these work because 0 is a valid value for all pointer types (NULL), and false evaluates to 0.

What you want to do is make the variable 'a' exist as long as your app is running. You can make it a global, or allocate it at startup and de-allocate it on exit, whatever.

cannyshammy
cannyshammy
cheers coder mate i have just discovered exactly the same thing myself just before i read your post and have now solved the problem.

After looking at some of the SDK samples i found out this is how you put the data in.

#define MZE_VIEWCOMBO	0#define MAZEVIEW 0#define PLAYERVIEW 1#define FPVIEW 2m_Interface.AddComboBox( MZE_VIEWCOMBO, 0, 0, 150, 32 );m_Interface.GetComboBox( MZE_VIEWCOMBO )->AddItem( L"Maze View", (void*)MAZEVIEW );m_Interface.GetComboBox( MZE_VIEWCOMBO )->AddItem( L"Player View", (void*)PLAYERVIEW );m_Interface.GetComboBox( MZE_VIEWCOMBO )->AddItem( L"First Person View", (void*)FPVIEW );


Because i have defined those values before the class their data will stay in memory untill the app closes, im not sure how doing (void*)DEFINITION works tho as all i am doing is pasing it a constant value not a pointer /shrug but it works...

And to get the data back:

DXUTComboBoxItem *pItem =NULL;	int a = 0;pItem = ((CDXUTComboBox*)pControl)->GetSelectedItem();a = (int) PtrToInt(pItem->pData);


That PtrToInt() macro is a sneaky little trick i saw used in the "HDRFormats" sample in the SDK ;)

Well thanks for all your time and help you guys, i hope this can help others now :)

Mike

this topic can now be renamed [SOLVED] ;)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.