# Stupid struct errors

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

## Recommended Posts

##### Share on other sites
For starters you might want to include a semi colon in the header...

That's all that stood out to me from a real quick glance, but give that a shot anyway.

 Waait... What's this about including a cpp? I think you have these backwards... The Cpp file should include the header, and no, it would not surprise me if this was screwing up the compiler.

Should be something more like:

#include "Globals.h"

{
cout <<"=========\n";
}

##### Share on other sites
Thanks alot man! :) Now I only have the struct type redefinition error left. And I still don't know how to put my code into those neat textboxes here on the forums. Anyone?

##### Share on other sites

struct mystruct{};
#endif

As to the code boxes, use [ code ] and [ /code ] tags, minus the spaces.

##### Share on other sites
Thanks for the codebox tip:D

All the errors are gone now, but I still get a link error:

--------------------Configuration: main - Win32 Debug--------------------Compiling...main.cppMainMenu.cppLinking...MainMenu.obj : error LNK2005: "struct items backpack_items" (?backpack_items@@3Uitems@@A) already defined in main.objDebug/main.exe : fatal error LNK1169: one or more multiply defined symbols foundError executing link.exe.

I tried to put the ifdefs in the global.h file, but that didn't work:\

##### Share on other sites
Do NOT define variables in a header. Define variables in a cpp file only, and to make sure everybody knows that the variable exists "define" the variable in a header with the extern keyword.

Instead of making a struct create typedef:

// Globals.h#ifndef _GLOBALS_H_#define _GLOBALS_H_typedef struct{int numItems;char weapon[100];char shield[100];char food[100];} type_backpack_items;extern type_backpack_items backpack_items;#endif

// Globals.cpp#include "globals.h"type_backpack_items backpack_items;

[Edited by - vNistelrooy on October 16, 2004 12:51:24 PM]

##### Share on other sites
Well now I did that and here's the result:

--------------------Configuration: main - Win32 Debug--------------------Compiling...main.cppc:\mats\programs\mud\globals.cpp(1) : error C2146: syntax error : missing ';' before identifier 'backpack_items'c:\mats\programs\mud\globals.cpp(1) : fatal error C1004: unexpected end of file foundMainMenu.cppc:\mats\programs\mud\globals.cpp(1) : error C2146: syntax error : missing ';' before identifier 'backpack_items'c:\mats\programs\mud\globals.cpp(1) : fatal error C1004: unexpected end of file foundGlobals.cppC:\Mats\Programs\MUD\Globals.cpp(1) : error C2146: syntax error : missing ';' before identifier 'backpack_items'C:\Mats\Programs\MUD\Globals.cpp(1) : fatal error C1004: unexpected end of file foundError executing cl.exe.main.exe - 6 error(s), 0 warning(s)

BTW: What does extern mean?
BTW: I typed "extern type_backpack_items backpack_items;" instead of "type_backpack_items backpack_items;" in the globals.cpp file, and that got rid of some errors.

##### Share on other sites
Are you including Globals.cpp or .h from the other source files?!

##### Share on other sites
Hmm... it seems I made it work. I did the mistake of including Globals.cpp! But what does extern mean?

##### Share on other sites
Quote:
 Original post by Afr0m@nHmm... it seems I made it work. I did the mistake of including Globals.cpp! But what does extern mean?

Look in MSDN. It just tells the compiler that the variable is defined somewhere else, and the linker resolve the location of the defination.
I've update the code above, hope you are doing it this way.

##### Share on other sites
I don't have MSDN:( It compiles nicely, so I don't see the point in putting the GLOBAL_H ifdefs in there. But thanks alot anyway:)

##### Share on other sites
I can't initialize my vars now for some reason:((( And thanks for the MSDN tip btw:))) I tried to do this:

#include "Globals.h"extern type_backpack_items backpack_items;bacpack_items.numItems = 0;

but then I got this:

--------------------Configuration: main - Win32 Debug--------------------Compiling...Globals.cppC:\Mats\Programs\MUD\Globals.cpp(5) : error C2143: syntax error : missing ';' before '.'C:\Mats\Programs\MUD\Globals.cpp(5) : error C2501: 'bacpack_items' : missing storage-class or type specifiersC:\Mats\Programs\MUD\Globals.cpp(5) : error C2143: syntax error : missing ';' before '.'Error executing cl.exe.main.exe - 3 error(s), 0 warning(s)

I'm so confused right now:(

##### Share on other sites
It's beacuse of how you're delcaring your struct. When you do something like this:
struct {    crap;} someName;

then someName becomes an INSTANCE of that struct. You have just declared a variable. Move it to the top where it will live happy:
struct someName{    crap;};

Now you can make variables of type someName.

##### Share on other sites
That didn't work:(

##### Share on other sites
Ok, try this:

in your Globals.h file, you should have the struct definition, and the line extern blah blah blah. In one of your .cpp files, you should have an actual declaration of the struct, without the extern keyword. That is your actual copy of the variable. Underneath that declaration, initialize all of the data.

--Globals.h--
typedef struct{  ...} type_backpack_items;extern type_backpack_items backpack_items;

--Main.cpp--
#include "Globals.h"type_backpack_items backpack_items;backpack_items.blah = something;etc...

##### Share on other sites
I still get errors:

--------------------Configuration: main - Win32 Debug--------------------Compiling...Globals.cppC:\Mats\Programs\MUD\Globals.cpp(5) : error C2143: syntax error : missing ';' before '.'C:\Mats\Programs\MUD\Globals.cpp(5) : error C2501: 'bacpack_items' : missing storage-class or type specifiersC:\Mats\Programs\MUD\Globals.cpp(5) : error C2143: syntax error : missing ';' before '.'Error executing cl.exe.main.exe - 3 error(s), 0 warning(s)

:'(

##### Share on other sites
C:\Mats\Programs\MUD\Globals.cpp(5) : error C2501: 'bacpack_items' : missing storage-class or type specifiers

make sure you didn't spell bacpack wrong, when you declared it as backpack.

##### Share on other sites
Sheeeze... I corrected it to this now:

backpack_items.numItems = 0;

but then I got 4 errors! :'(

--------------------Configuration: main - Win32 Debug--------------------
Compiling...
Globals.cpp
C:\Mats\Programs\MUD\Globals.cpp(5) : error C2143: syntax error : missing ';' before '.'
C:\Mats\Programs\MUD\Globals.cpp(5) : error C2501: 'backpack_items' : missing storage-class or type specifiers
C:\Mats\Programs\MUD\Globals.cpp(5) : error C2371: 'backpack_items' : redefinition; different basic types
c:\mats\programs\mud\globals.h(20) : see declaration of 'backpack_items'
C:\Mats\Programs\MUD\Globals.cpp(5) : error C2143: syntax error : missing ';' before '.'

##### Share on other sites
Can you post your exact source from the Globals.h file dealing with the definition of type_backpack_items and the portion of whatever .cpp file is giving you those errors? It could be something small we've all overlooked.

##### Share on other sites
//Globals.cpp

#include "Globals.h"

type_backpack_items backpack_items;

backpack_items.numItems = 0;

================================================================

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Globals.h
//
// Purpose: The only header file that MUST be included in ALL the source files.
// Defines lots of global variables and structs.
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <iostream.h>

typedef struct
{
int numItems;
char weapon[100];
char shield[100];
char food[100];
} type_backpack_items;

extern type_backpack_items backpack_items; // Extern tells the compiler that this variable is declared elsewhere.
// The compiler lets the linker take care of finding the file that the
// variable is declared in.

##### Share on other sites
You can't call the statement backpack_items.numItems=0 unless it's inside a function somewhere.

##### Share on other sites
Quote:
 Original post by Afr0m@nThanks alot man! :) Now I only have the struct type redefinition error left. And I still don't know how to put my code into those neat textboxes here on the forums. Anyone?

Dude, cant' you just read the faq link on top of the page. It tells you how to format your code. No wonder that you have soooo much problems programming if you even can not read the faq. My suggestion is to just stop programming, sell you comnputer and go into sanitation bussiness.

##### Share on other sites
How could I forget? :( I'm so stupid sometimes!!! Well thanks to everyone for the help:D You guys are the best:D