• 11
• 27
• 9
• 20
• 31

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

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

## 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:

[source lang="cpp"]

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

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

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

{
[...]
}

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

[...]
[...]

//fill in the struct

//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 on other sites

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 on other sites

[quote name='Serapth' timestamp='1342710720' post='4960965']

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

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"]
[/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 on other sites

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 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: Serapth beat me to it

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

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

Cheers!

Fred

##### 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.

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"]
[/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:

##### 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:

[source lang="cpp"]

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
...
[/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]