Jump to content
  • Advertisement
Sign in to follow this  
Afr0m@n

Stupid struct errors

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

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
Advertisement
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
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!