Jump to content
  • Advertisement
Sign in to follow this  
George88

Not letting me pass structure to function

This topic is 2601 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I'm currently having some difficulty passing in some struct data to a function. I'm doing some basic DirectX game tutorials and have just got finished making Pong. In the project there is a [font="Courier New"]GAMEWINDOW [/font]struct with game window information such as [font="Courier New"]hWnd, int width, int height, bool windowed[/font]. The struct has a function [font="Courier New"]GAMEWINDOW()[/font], which defines these variables.

This struct is passed in to various functions throughout the code using the following format:


Prototype:
[font="Courier New"]void init_direct_input(HINSTANCE hInstance, GAMEWINDOW* gw);[/font][/quote]

Function declaration:
[font="Courier New"]void init_direct_input(HINSTANCE hInstance, GAMEWINDOW* gw)
{
}
[/font][/quote]
It works fine with that function and I can use the arrow operator to take data from the struct, such as[font="Courier New"] gw->height;[/font]

So, being the newby that I am, I thought I could just use "[font="Courier New"]GAMEWINDOW* gw[/font]" and pass it in to any function I liked. This isn't working. What I want to do is pass in this struct to a different function so I may take advantage of the "[font="Courier New"]INT WIDTH[/font]" and "[font="Courier New"]INT HEIGHT[/font]" definitions. I know I could just code these in manually, but if I do that and change the window size, I have to go around changing multiple values.

Anyway, when I add "[font="Courier New"]GAMEWINDOW* gw[/font]" to the function prototype, then try passing it in to the function CALL, I get a red line under it and compiler errors. The "Popup" error says: [font="Courier New"]ERROR: TYPE NAME NOT ALLOWED[/font] and the compiler error says "[font="Courier New"]ILLEGAL USE OF THIS TYPE AS AN EXPRESSION[/font]".

I have tried every way I can to get this function to let me pass it the struct such as passing it [font="Courier New"]&gw[/font], [font="Courier New"]GAMEWINDOW gw[/font], [font="Courier New"]GAMEWINDOW gw[][/font], [font="Courier New"]GAMEWINDOW* gw[][/font], nothing works. Of course, I know passing an array is not going to work anyway, but it was worth trying.

What is going on here? I don't understand why I am not allowed to pass the struct in to this function, as I did previously with other functions. I am using Visual C++ Express (2010).


Thanks for reading :).

Share this post


Link to post
Share on other sites
Advertisement
When you passing gw to a function you should not mention the type. The function call will look something like this: init_direct_input(hInstance, gw);

Share this post


Link to post
Share on other sites
Could you post the exact code that you're using that causes the error?


[edit] Ninja'd with the likely solution :-)

Share this post


Link to post
Share on other sites

I have tried every way I can to get this function to let me pass it the struct such as passing it [font="Courier New"]&gw[/font], [font="Courier New"]GAMEWINDOW gw[/font], [font="Courier New"]GAMEWINDOW gw[][/font], [font="Courier New"]GAMEWINDOW* gw[][/font], nothing works. Of course, I know passing an array is not going to work anyway, [color="#ff0000"]but it was worth trying.

No it wasn't. Randomly guessing at the solution is very bad, for this reason: It might actually work. Code that is wrong and doesn't compile is good. It means that you are 100% sure something is wrong. Code that is wrong and does compile is extremely bad, because you think it works fine, but it secretly is puking over your program's innards. It's always far better to be bleeding on the outside where you can see it and stop the blood flow, then to be internally bleeding and not know about it. smile.gif

It may take a little time and experience, but it's very important that you understand the differences between regular variables, pointers, and references. Otherwise, things might actually 'work' but will suddenly stop working when you run your program for the 92nd time, and it'd be harder to track because the area of code that is causing the problem was written months ago, and 'has never given me trouble before, so it can't be the problem'. wink.gif

Share this post


Link to post
Share on other sites
Hello guys,

First, thank you for your responses. Servant, you're right. It was an unwise and bad idea to try random things, I was just at a loss and couldn't think of what to do. I won't be making that mistake again. Anyway, I've gotten the code working as I wanted and will post the solution in the event anyone comes across this thread in the future.

So basically I was trying to pass in a pointer to my struct [font="Courier New"]GAMEWINDOW [/font]so I could take advantage of its properties. I was using [font="Courier New"][color="#0000ff"][font="Arial"][color="#000000"]the following call format:[/font] func(GAMEWINDOW* gw);[/font] and hoping this was going to act as the definition/declaration of a new struct. This was not the case. All I needed to do was create a new struct within the function, so this was my original code:


void main_loop(void)
{
init_game(game_object, &gw); [non-working line]

while(handle_messages())
{
input(&input_data);
logic(game_object, &input_data);
render(game_object);
}
}


Which became:
void main_loop(void)
{
GAMEWINDOW gw;
init_game(game_object, &gw);

while(handle_messages())
{
input(&input_data);
logic(game_object, &input_data);
render(game_object);
}
}


A simple fix to a problem that shouldn't have happened :-). Thanks again, it it were not for your replies spurring on thought, I'd have overlooked this one.

Hopefully I won't make this mistake again.

Share this post


Link to post
Share on other sites

stuff


So wait, Which 'gw' object were you using in your other function? The way you do it now may not necessarily reference the same game window; in fact it probably doesn't, but I don't know how much extra stuff has been cut out to save space. You will probably get the wrong height/width values because of this.

Edit: it might be better to trigger a window size changed event that resets what you want and just pass the width and height by value into the function if all you need is the window height and width. It feels like you are giving your function too much information to me. Not necessarily bad, but could get bad eventually and if you only need 2 numbers probably not worth giving access to the whole object.

edit: probably should look into passing it in by const reference also if all you want is width and height. Save your program from people breaking it later. ("people" may refer to you in 6 months).

Share this post


Link to post
Share on other sites
Hi,

There are 7 source files and 5 headers so I can't paste all the code. Also, the code is from the directxtutorial.com premium area so I don't think posting it here would be allowed.

But I just created a new one. The struct definition is:

struct GAMEWINDOW
{
HWND hWnd;
int width, height;
bool windowed;

GAMEWINDOW()
{
hWnd = NULL;
width = 1024;
height = 768;
windowed = true;
}
};


The values are always the same.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!