gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > request for member `...' in something not a structu

#12206 - RaBBi - Mon Nov 03, 2003 10:33 pm

Hi,

Few days ago, I wrote a topic about cut-scenes management (not initially but it changed to it after discussion with Gopher, that I thank ^^, and others).

Then I started to wrote a system.
This needs structs like the following.

One which stores the datas corresponding to the event type.
One which stores the event itself.
The first is used in a union inside the second.

You see it next :
(typedefs.h file)

Code:

typedef union UEventData {
    struct TEventPrintf
       {
       
           u8 startX, startY, timePause, vitesse;
            const char *Fmt;

       } TEventPrintf;
       
       // some other structs

} UEventData;

typedef struct TEvent
{
    // donn?es communes :
    // liste des events qui doivent etre ? true pour que l'?venement s'enclenche
    // 5 maxi
    u8 eventsTrueArray[5];
    
    // 1 indicateur de l'activit? de l'event
    bool eventActive;
    
    // 1 indicateur de la terminaison de l'event
    // il est mis ? TRUE quand la fonction renvoie TRUE
    bool eventDone;

    // 1 union qui contient des structures EventType
    struct TEventType {
       
            // donn?es propres ? l'event :
            // elles seront transmises/utilis?es par la fonction associ?e
            UEventData EventData;
           
            // un pointeur vers la fonction utilis?e par
            // cet ?venement
            // celle-ci doit renvoyer un bool
            bool (*EventFunc)(UEventData *Data, ...);

    } TEventType;
    
 } TEvent;


Then I use a text function (for a dialog event).
So the (*EventFunc) is pointing on it.
(texts.c file)

Code:

bool m_printf_Event(UEventData *Data, ...)


But when I compile, an error message says :

Code:

request for member `TEventPrintf' in something not a structure or union


When I try to use :

Code:

Data.TEventPrintf.Fmt


for example.

I precise that typedefs.h is included well by the texts.c file.
_________________
Sorry for my poor english, but it would be worst for you to understand me if I speak in my native language, French ^^

#12207 - spooo - Mon Nov 03, 2003 10:49 pm

> Data.TEventPrintf.Fmt

You may try (Data->TEventPrintf).Fmt instead.

(You try to access data like if it was a union/structure, but Data is a pointer, that's the error message's meaning).

#12208 - RaBBi - Mon Nov 03, 2003 11:15 pm

Thank you !

it works now ^^

But I've got another problem now, a warning.

It deals with the text function.
In fact, the original version of this function takes a string with format, like vprintf for example.

That's the prototype of this previous version :
Code:

bool m_printf(u8 startX, u8 startY, u8 timePause, u8 vitesse, const char *Fmt, ...)


you see the last one parameter, Fmt, is used like this :

Code:

...
va_list ap;

        // D'abord affiche le texte format?
        va_start ( ap, Fmt);
        vsprintf(Buff, Fmt, ap);
        va_end(ap);
...


But with the new function I use in this event management, this parameter is stored into a struct UEventData.
And I pass the struct to the new function.
So I get a warning on this line :

Code:

va_start ( ap, Data.TEventPrintf.Fmt);


It says me :
Code:

warning: second parameter of `va_start' not last named argument


I was prepared to this type of warning.
But for now, I cannot resolve it.

My purpose is to be able to store string and its format into UEventData structure.
In order to do :

textFunction(UEventData_struct);

like I'll do :

textFunction(x,x,x,"this is my demo number %d", demoNumber);
_________________
Sorry for my poor english, but it would be worst for you to understand me if I speak in my native language, French ^^

#12219 - Gopher - Tue Nov 04, 2003 8:01 am

I don't use the "..." variable parameter syntax much, so I can't speak very confidently here. But, I don't see why you need to pass the string Fmt to va_start.

In the first example
Code:

  va_start ( ap, Fmt);


you passed Fmt not because va_start actually uses the string, but because it was the last explicit parameter. So in your new example, where the parameter "char* Fmt" has been replaced with "UEventData Data", you don't need to pass Data.TEventPrintf.Fmt, but just Data.

If I'm wrong, someone correct me, as I said the "..." is not something I've used often.
_________________
"Only two things are infinite: the universe, and human stupidity. The first is debatable." -Albert Einstein

#12230 - RaBBi - Tue Nov 04, 2003 8:59 pm

Yes you're right.

I didn't need to pass [...].Fmt, to specify the last fixed parameter, in fact I cannot.
Cause the last fixed parameter is the whole struct Data.

So, with reflexion and your advice, I just did that :

Code:

va_start ( ap, Data);
vsprintf(Buff, (Data->TEventPrintf).Fmt, ap);


And it works ^^

But now, I would be able to store string and format is the UEventData.
This means, store "you have %d victories and %d defeats", vicTotal, defTotal
But in the structure.
_________________
Sorry for my poor english, but it would be worst for you to understand me if I speak in my native language, French ^^

#12259 - Gopher - Wed Nov 05, 2003 9:29 pm

If you want to store the variable-length array of parameters in a structure, there's only two options I can see.

One is to have a fixed-length array of void pointers to the parameters, with the length being set to the maximum number of parameters you may have in any case.

The other is to use a dynamic allocation to hold them, either something like a linked list or a dynamic array of pointers.

Either approach could work, but both are fairly expensive; myself, it seems like a level of 'convenience programming' I generally avoid on GBA. By that I mean it doesn't really make the program better, it just makes the source easier to modify/extend, but without proper optimization the price may be high.
_________________
"Only two things are infinite: the universe, and human stupidity. The first is debatable." -Albert Einstein

#12345 - MumblyJoe - Sat Nov 08, 2003 2:17 pm

Any chance of getting that code with english comments? Just curious...
_________________
www.hungrydeveloper.com
Version 2.0 now up - guaranteed at least 100% more pleasing!