Sign in to follow this  
Afr0m@n

Stupid struct errors

Recommended Posts

Afr0m@n    100
I've created a struct and put it in a global header file (the only header file included by all the source files). As far as I can see, the struct is perfectly legal, but I still get alot of errors. Can someone tell me why? ================================================================ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // 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> struct items { int numItems; char weapon[100]; char shield[100]; char food[100]; } backpack_items; ================================================================ ================================================================ // MainMenu.h #include "MainMenu.cpp" void MainMenu() ================================================================ // main.cpp #include "Globals.h" #include "MainMenu.h" int main() { cout <<"MUD by Mats Vederhus!\n"; cout <<"=====================\n"; MainMenu(); return(0); } ================================================================ // MainMenu.cpp #include "Globals.h" void MainMenu() { cout <<"MAIN MENU\n"; cout <<"=========\n"; } ================================================================ ================================================================ --------------------Configuration: main - Win32 Debug-------------------- Compiling... main.cpp c:\mats\programs\mud\globals.h(13) : error C2011: 'items' : 'struct' type redefinition c:\mats\programs\mud\main.cpp(4) : warning C4518: 'int ' : storage-class or type specifier(s) unexpected here; ignored c:\mats\programs\mud\main.cpp(4) : error C2146: syntax error : missing ';' before identifier 'main' c:\mats\programs\mud\main.cpp(4) : fatal error C1004: unexpected end of file found MainMenu.cpp Error executing cl.exe. main.exe - 3 error(s), 1 warning(s) ================================================================ BTW: Can someone PLEASE tell me how to organize my code into those neat textboxes when I post?

Share this post


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

// MainMenu.h

#include "MainMenu.cpp"

void MainMenu();

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

[edit] 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:

// MainMenu.h
void MainMenu();

// MainMenu.cpp
#include "MainMenu.h"
#include "Globals.h"

void MainMenu()
{
cout <<"MAIN MENU\n";
cout <<"=========\n";
}

Share this post


Link to post
Share on other sites
Afr0m@n    100
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 this post


Link to post
Share on other sites
It means the header's being included repeatedly. Your header should have inclusion guards like this:

#ifndef MYHEADER
#define MYHEADER
struct mystruct{};
#endif

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

Share this post


Link to post
Share on other sites
Afr0m@n    100
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.cpp
MainMenu.cpp
Linking...
MainMenu.obj : error LNK2005: "struct items backpack_items" (?backpack_items@@3Uitems@@A) already defined in main.obj
Debug/main.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.


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

Share this post


Link to post
Share on other sites
vNistelrooy    140
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 this post


Link to post
Share on other sites
Afr0m@n    100
Well now I did that and here's the result:


--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
c:\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 found
MainMenu.cpp
c:\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 found
Globals.cpp
C:\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 found
Error 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 this post


Link to post
Share on other sites
vNistelrooy    140
Quote:
Original post by Afr0m@n
Hmm... 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 this post


Link to post
Share on other sites
Afr0m@n    100
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.cpp
C:\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 specifiers
C:\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 this post


Link to post
Share on other sites
wendigo23    512
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 this post


Link to post
Share on other sites
pi_equals_3    517
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 this post


Link to post
Share on other sites
Afr0m@n    100
I still get 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: 'bacpack_items' : missing storage-class or type specifiers
C:\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 this post


Link to post
Share on other sites
stormrunner    720

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 this post


Link to post
Share on other sites
Afr0m@n    100
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 this post


Link to post
Share on other sites
pi_equals_3    517
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 this post


Link to post
Share on other sites
Afr0m@n    100
//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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Afr0m@n
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?


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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this