Jump to content
  • Advertisement
Sign in to follow this  
iam73

Syntax Error C2061 while declaring a struct as a function argument...

This topic is 2340 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

Being new at programming, I'm really pulling my hairs on this one. Been searching and trying all kind of moves since yesterday morning but I'm at a point where I need directions.

The problem I have is at compile time, while trying to pass a struct as a function argument - Visual Studio 2010 Ultimate is giving me a C2061 Syntax Error on my struct identifier in the function prototype.

Here's my shortened code:

reader.h
[source lang="cpp"]

typedef struct _fntHeader{
char FontTag[8]; // (5 bytes) Data Identifier 'BBF2'
unsigned long misc; // (4 bytes) Future use
char desc[50]; // (50 bytes) Artist comments
int iFirstASCIIId; // (4 bytes) First ASCII Id
int iLastASCIIId; // (4 bytes) Last ASCII Id
int iBitDepth; // (4 bytes) BitDepth of Font
int iMaxFontWidth; // (4 bytes) Widest font size
int iMaxFontHeight; // (4 bytes) Tallest font size
int iCharSpacing; // (4 bytes) Extra layout spacing between characters
int iCharLineSpacing; // (4 bytes) Extra layout spacing between lines (rows)
short wTransColor; // (2 bytes) transparent color (packed)
long lCharacterBufferSize; // (4 bytes) Size of font data
} fntHeader;

[/source]
GUI.h (this is where VS complains about a syntax error on fntHeader)
[source lang="cpp"]

void displayFNTinformation(HWND hwnd, fntHeader fontHeader);

[/source]
GUI.cpp
[source lang="cpp"]

#include "reader.h"

void displayFNTinformation(HWND hwnd, fntHeader fontHeader)
{
[...]
}

[/source]
winmain.cpp
[source lang="cpp"]

[...]
#include "reader.h"
[...]

//fill in the struct
fntHeader fontFile = readFNTfile();

//show FNT information on GUI
displayFNTinformation(hwnd, fontFile);

[...]

[/source]

Now obviously VS knows about fntHeader - otherwise it'd throw a unknown identifier error instead, but I can't find what the syntax issue is in this case...

What do you guys think?

Share this post


Link to post
Share on other sites
Advertisement

Does GUI.h include reader.h?


It doesn't, but trying to include reader.h in gui.h gives me redefinition errors, so I tried including include guards but still having the syntax errors on fntHeader...

Share this post


Link to post
Share on other sites

[quote name='Serapth' timestamp='1342710720' post='4960965']
Does GUI.h include reader.h?


It doesn't, but trying to include reader.h in gui.h gives me redefinition errors, so I tried including include guards but still having the syntax errors on fntHeader...
[/quote]

Well Gui.h needs to know about the structure, so its either included or forward declared. If you are getting redefinition errors, your header guard isn't working or you are defining it again somewhere.

Share this post


Link to post
Share on other sites

Well Gui.h needs to know about the structure, so its either included or forward declared. If you are getting redefinition errors, your header guard isn't working or you are defining it again somewhere.


Serapth, you made my day.

I couldn't get this to work with include guards and didn't know about forward declaration... Quick googling led me to loads of information on forward declaration, I fixed my problem within the next 5 minutes by adding this to GUI.h:
[source lang="cpp"]
typedef struct _fntHeader fntHeader;
[/source]

Thank you so much for the tip!

Now one last question, is it ok to pass the struct as an argument or should I pass a pointer instead?
The struct is only read by GUI.cpp, no modifications done.

So much to learn (sigh...)

Share this post


Link to post
Share on other sites
Well the answer to your question all comes down to your requirements.

One thing to keep in mind, when you pass a parameter by value ( for example MyFunction(mystruct val1) ), you are creating a new copy of it, that will live only so long as the function does.

So consider the following pseudo code:

myFunction(mystruct val1)
{
// At this point val1 is a complete copy of exampleStruct
val1.value = 43;
} // <- at this point, val1 perishes and no longer exists
{
mystruct exampleStruct;
exampleStruct.value = 42;
myFunction(exampleStruct);
cout << exampleStruct.value; // prints 42
}



Chances are, what you want is a reference. Like this ( the only difference is an & in the function declaration

myFunction(mystruct & val1)
{
val1.value = 43;
} // val1 IS exampleStrcut, so changes to val1 are changed to the source as well
{
mystruct exampleStruct;
exampleStruct.value = 42;
myFunction(exampleStruct);
cout << exampleStruct.value; // prints 43
}




As a general rule, until you know why to do otherwise, if you want to change the original value, always pass by reference, otherwise pass by value.

Share this post


Link to post
Share on other sites
you can pass as an argument, i would recommend using a const reference, like const struct& var. This saves memory and time of having to copy your struct onto the stack
if you want you can also use a pointer, but const references relieve you of having to use pointer syntax

Edit: sleep.png Serapth beat me to it

PS: instead of using a typedef, you could have just removed the "_" from _fntHeader tongue.png Edited by arkane7

Share this post


Link to post
Share on other sites
Thank you very much for taking the time to help me today, it was very useful!

Cheers!

Fred

Share this post


Link to post
Share on other sites

[quote name='Serapth' timestamp='1342712217' post='4960973']
Well Gui.h needs to know about the structure, so its either included or forward declared. If you are getting redefinition errors, your header guard isn't working or you are defining it again somewhere.


Serapth, you made my day.

I couldn't get this to work with include guards and didn't know about forward declaration... Quick googling led me to loads of information on forward declaration, I fixed my problem within the next 5 minutes by adding this to GUI.h:
[source lang="cpp"]
typedef struct _fntHeader fntHeader;
[/source]

Thank you so much for the tip!

Now one last question, is it ok to pass the struct as an argument or should I pass a pointer instead?
The struct is only read by GUI.cpp, no modifications done.

So much to learn (sigh...)
[/quote]

oops, I missed this on the first read through. You have done your forward declaration wrong here. You do not need to typedef, in fact, you probably do not want to.

When forward declaring you are basically just saying "hey I have this struct/class named XXX and I will define it later". All you need to do is add:

struct fndHeader;

Share this post


Link to post
Share on other sites
[sub]

Being new at programming, I'm really pulling my hairs on this one. Been searching and trying all kind of moves since yesterday morning but I'm at a point where I need directions.

The problem I have is at compile time, while trying to pass a struct as a function argument - Visual Studio 2010 Ultimate is giving me a C2061 Syntax Error on my struct identifier in the function prototype.

Here's my shortened code:

reader.h
[source lang="cpp"]

typedef struct _fntHeader{
char FontTag[8]; // (5 bytes) Data Identifier 'BBF2'
unsigned long misc; // (4 bytes) Future use
char desc[50]; // (50 bytes) Artist comments
int iFirstASCIIId; // (4 bytes) First ASCII Id
int iLastASCIIId; // (4 bytes) Last ASCII Id
int iBitDepth; // (4 bytes) BitDepth of Font
int iMaxFontWidth; // (4 bytes) Widest font size
int iMaxFontHeight; // (4 bytes) Tallest font size
int iCharSpacing; // (4 bytes) Extra layout spacing between characters
int iCharLineSpacing; // (4 bytes) Extra layout spacing between lines (rows)
short wTransColor; // (2 bytes) transparent color (packed)
long lCharacterBufferSize; // (4 bytes) Size of font data
} fntHeader; <-----------------------------Do you want a variable called fntHeader???
...
[/sub]


[sub]i believe fntHeader is a VARIABLE of type _fntHeader. i think this might be a possible issue. I don't know if that was your intention or not but putting a name at the end also declares an object of that struct type. What i think you want is to have the struct be called fntHeader. otherwise whenevr you use fntHeader there might be problems of interpretting it as the variable[/sub]


[sub]what i think you want is this:[/sub]

[sub]struct fntHeader
{[/sub]
[sub]//your code here
};[/sub]

[sub]As for your issue in the header file of GUI, do what Serapth said, just declare it as struct fntHeader; inside GUI.h, just dont use it inside the header. do all your implementations and uses of fntHeader inside GUI.cpp while including reader.h[/sub] Edited by arkane7

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!