Sign in to follow this  
Schade

multiple definition

Recommended Posts

Hello im having a problem with my functions,when i remove the new code i put in it works fine.Well heres my code too see if anyone can figure this out. Thank Main.cpp
#include "Library.h"

int main(void)
{
     //Generic Welcome Screen
     
     cout << "\n\t\tWelcome to Relic's of Sanguine\n";
     cout << "\t\tProgrammed by Derric Schade\n\n";
     
     cout << "The world of Sanguine has collasped by the orc's";
     
     CityGate();
     
     cout << "\n\nThank your for playing Relic's of Sanguine\n\n";
     
{
 }
     system("pause");
     return 0;
     }


CityGate.cpp
#include "Library.h"

// CityGate


bool CityGate()
{
     int choice = 0;
     
      while (choice != 10)
     {
     cout << " You stand at the gate of the city of izmore ther are\nserveral road's you can take.\n\n";
     cout << " There is a lady behind you asking for some help.\n\n";
     
     cout << "Which path do you want to choose?\n";
     cout << "1: Town\n";
     cout << "2: Summoning Portal\n";
     
    if (!glcomplete)
       cout << "3: Grasslands (QUEST)\n";
    
    if (glcomplete && !fcomplete)
       cout << "4: Forest Road (QUEST)\n";
       
    if (fcomplete && !scomplete)
       cout << "5: Swamp Road (QUEST)\n";
       
    if (scomplete && !gcomplete)
       cout << "6: Graveyard Road (QUEST)\n";
       
    if (gcomplete && !ccomplete)
       cout << "7: Castle Road (QUEST)\n";
       
    cout << "10: Exit Game\n";
    
    cout << ">";
    
    cin >> choice;
    
}
     
     return true;
 }


Battle.cpp
// Battle

#include "Library.h"

bool Battle()
{
     return false;
 }


Town.cpp
#include "Library.h"
// Town

bool Town()
{
     return true;
 }

Library.h
#include <iostream>

using namespace std;

#include "Globals.h"


Globals.h ok so here is where i think im having the problem. It's something to do with the glcomplete,fcomplete etc. Giving me multiple definition error.
#ifndef GLOBAL_DEFINE
#define GLOBAL_DEFINE

// Prototypes

bool CityGate();
bool Town();

//Global Variables

bool glcomplete = false;
bool fcomplete = false;
bool scomplete = false;
bool gcomplete = false;
bool ccomplete = false;

#endif


Ok so thats all my code an here are the errors that shows up.
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Projects\First Text based game\Makefile.win"
Executing  make...
make.exe -f "C:\Dev-Cpp\Projects\First Text based game\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  
-I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2
/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"   

g++.exe -c CityGate.cpp -o CityGate.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2
/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include
/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"   

g++.exe main.o Battle.o Town.o CityGate.o  -o "Relics of Sanguine.exe" 
-L"C:/Dev-Cpp/lib"  

CityGate.o(.bss+0x0):CityGate.cpp: multiple definition of `glcomplete'
main.o(.bss+0x0):main.cpp: first defined here
CityGate.o(.bss+0x1):CityGate.cpp: multiple definition of `fcomplete'
main.o(.bss+0x1):main.cpp: first defined here
CityGate.o(.bss+0x2):CityGate.cpp: multiple definition of `scomplete'
main.o(.bss+0x2):main.cpp: first defined here
CityGate.o(.bss+0x3):CityGate.cpp: multiple definition of `gcomplete'
main.o(.bss+0x3):main.cpp: first defined here
CityGate.o(.bss+0x4):CityGate.cpp: multiple definition of `ccomplete'
main.o(.bss+0x4):main.cpp: first defined here
collect2: ld returned 1 exit status

make.exe: *** ["Relics] Error 1

Execution terminated
[Edited by - Zahlman on February 24, 2010 3:22:06 PM]

Share this post


Link to post
Share on other sites
When you include global.h (via library.h) in each of the cpp files they will all define:

bool glcomplete = false;
bool fcomplete = false;
bool scomplete = false;
bool gcomplete = false;
bool ccomplete = false;

That is why you are getting multiple definitions.
What you want to do is (in globals.h):
Declare the variables as extern to tell all the files that include this file
that a variable of this name exists but is defined some other place.

extern bool glcomplete;
extern bool fcomplete;
extern bool scomplete;
extern bool gcomplete;
extern bool ccomplete;



And then in one of the cpp files you do the definition.
For example in Main.cpp:

bool glcomplete = false;
bool fcomplete = false;
bool scomplete = false;
bool gcomplete = false;
bool ccomplete = false;

It's really the same thing as function prototyping, you wouldn't write the the body of a function in a header-file included by more then one .cpp/.c, if you did you would get multiple definitions of that function.
That is why you do the prototyping in the header, and then implement the function one time in a cpp/c file. If you don't implement the function and try to call it with only having the prototype the linker will yield unresolved symbols error. The same goes for extern variables that isn't defined.

Share this post


Link to post
Share on other sites
ok so i put

extern bool glcomplete;
extern bool fcomplete;
extern bool scomplete;
extern bool gcomplete;
extern bool ccomplete;

inside the globals.h an moved

bool glcomplete = false;
bool fcomplete = false;
bool scomplete = false;
bool gcomplete = false;
bool ccomplete = false;

to main.cpp i still get the erros, but if i remove that an keep globals.h with
the extern it works. an thank you iv been stumped on that for a little while.

Share this post


Link to post
Share on other sites
Copy-and-paste response for linker errors:

Read this until you know how to solve your problem.

If you have read the whole thing and think it does not explain how to solve your problem, you are almost certainly wrong. Read it again.

If you really, really still think the information isn't there, then post, but be prepared to explain in more detail, and be prepared for me to be very annoyed if I can show that the information really is in there.

Also, I posted a sticky in this forum to tell you not to just dump your code like that. There is functionality here for posting code and it is your responsibility to do so properly. I fixed your post, but don't do it again.

Share this post


Link to post
Share on other sites
Thanks you guys sorry it took a couple days to get back to this iv been busy, but it did work. an sorry zahlman i forgot about the [code] tag.

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