Stupid struct errors
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?
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";
}
// 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";
}
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?
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
#ifndef MYHEADER
#define MYHEADER
struct mystruct{};
#endif
As to the code boxes, use
and
tags, minus the spaces.
Thanks for the codebox tip:D
All the errors are gone now, but I still get a link error:
I tried to put the ifdefs in the global.h file, but that didn't work:\
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:\
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:
[Edited by - vNistelrooy on October 16, 2004 12:51:24 PM]
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]
Well now I did that and here's the result:
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.
--------------------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.
Hmm... it seems I made it work. I did the mistake of including Globals.cpp! But what does extern mean?
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement