multiple definition error

Im trying to get strings and vectors to be used in header files, but im not sure how to do that... Heres what I have:

playerdatabase.cpp:

1
2
3
4
5
6
7
8
9
10
#include <vector>
#include <string>

using namespace std;

string pname;
vector<string> inventnames;
vector<string> inventtypeid;
vector<string> inventmatid;
vector<string>::iterator search;                                                                         


playerdatabase.h:
1
2
3
4
5
6
            
extern std::string pname;                                             
extern std::vector<std::string> inventnames;
extern std::vector<std::string> inventtypeid;
extern std::vector<std::string> inventmatid;
extern std::vector<std::string>::iterator search;


main.cpp:
#include "playerdatabase.h"

anotherfile.cpp:
#include "playerdatabase.h"
I suppose in playerdatabase.h you should make a class with static members. Include it in playerdatabase.cpp and declare them there.

playerdatabase.h:
1
2
3
struct GLOBAL{
   static string str;
};


playerdatabase.cpp:
1
2
#include "playerdatabase.h"
string GLOBAL::str;
This is what i understand as your problem, I hope i am rite because haven't explained the problem well.

Problem-> you have created a header file which you hope to include in many files of the same program. It gives error that multiple definitions found.

Solution->

Seeing your code I can see that you haven't done any checking for previous definitions of the same header file.
So, what happens is , every where you include the header file "playerdatabase.h" the precompiler copy pasts the code from header file to your source file.
It works if your program is containing only one occurrence of "#include "playerdatabase.h" " macros. but in here you have it several times.
So, What happens is that your header file is copy pasted to the source file several times(one copy per #include "playerdatabase.h" ).
So, of course, when Pre-compiler gives the Pre-compiled source code to the actual C compiler it sees multiple occurrences of the same functions or variables or both.
What you have to do is make your pre-compiler check whether the header file is already copy pasted to the source code.

you can do it with a simple macro and always remember to do it for every header file you write in the future.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include <stdio.h>

#ifndef PLAYERDATABASE_H
#define PLAYERDATABASE_H

/* 
...
header file content ( all your code )
...
*/

#endif


What happens is when precompiler see the ifndef directive and it means "If PLAYERDATABASE_H is not defined do what you want to do to the codes below until you see endif directive". when the precompiler meets this header file for the first time during a compilation, as it havent defined any Macro Name called PLAYERDATABASE_H, the if condition becomes true,
and it continues to the next line.
Then the precompiler sees define directive which defines the macro name PLAYERDATABASE_H and defines to it self that there exists a Macro Name called PLAYERDATABASE_H.
then it will continue and copy all your code to the sourcefile until it meets endif.

when the precompiler meets the header file again, when it checks for the Macro Name PLAYERDATABASE_H, it sees that it is defined, and if condition becomes false, which makes the precompiler go to the endif location , without copy pasting your header again.

if there is some code after the endif it will of course copy paste it or follow any directive you given there too. macros are awsome stuff.

ps.
in case you dont know about the precompiler,
it is a part of the compilers, code is first passed to it to process all the macros and stuff. dont worry every C compiler has a precompiler.

I hope this long reply will help you better.
but if i misunderstood your problem, i am very sorry..
thanks
cheers..
Wait this makes no sense... I tried chaturas idea many times and it never worked... I decided to try it one more time and it suddenly works now... I must have fixed something else somewhere and got it to work. Thanks! And I know its probably not one of the greatest ideas to use vectors across files, but im going to just for testing purposes...

But anyways what was the stdio.h for?? I removed it and it works fine.
Last edited on
Oh i am sorry friend... i did that mistake.. i wrote #include <stdio.h> at there in the purpose of telling you that you can write all the include libraries on the head, before you write this macro, if you want to...

stdio.h is the standard Input Output handler library in C. stdlib.h and stdio.h are the most favorite ( or commonly used ) library file for "C" programmers. I am more of a C guy than C++, so thats what happened..

i think that iostream and stdio.h cannot be put together, you will have to use cstdio.h (substitute for stdio.h for C++) which is created for C++ and it contains the same stdio.h functionality... but i don't know about cstdio.h so i cant say anymore about it. i am sure there are good explanations around the internet. I think cstdio.h is slow because it contains OOP code because it is made for C++.

you can put your C++ favorite library iostream and other libraries in there if you want to.. or you can include those thing also inside the macro...

sorry for that trouble.. :)

i like stdio.h because it has very powerful tools like "Fprintf","Printf", "Scanf"... and many more...

stdlib.h has some serious stuff like, exit function, exit return values and also datatype converters such as Float to String...etc.

here are some links that helps you with them... (Opengroup.org is one of the best website for C documentation)
http://www.opengroup.org/onlinepubs/9699919799/basedefs/stdlib.h.html
http://www.cplusplus.com/reference/clibrary/cstdlib/
http://www.opengroup.org/onlinepubs/9699919799/basedefs/stdio.h.html
http://www.cplusplus.com/reference/clibrary/cstdio/

cheers...
Last edited on
Edit: Oops wrong forum, can moderator help me move to Lounge ? :P
Topic: Infrastructure Plumbing vs Business Application Developer

Without standard C++ STL, can you imagine writing your own quicksort, merge, partition etc algorithm yourself ? This is even before you can focus on the business logic. That is why standard C++ STL is born, it is a god-gift so that C++ developers can be business-centric focus like Java does too.

Without C++ STL, most likely C programs use some Open Source or commercial libraries for their "infrastructure plumbing code".

Truly speaking the openings for business application developers greatly outnumbered infrastructure plumbing developers at least in my country. How often does one get employed writing C++ compiler/linker versus a company in-house business application developer ?

I truly understand there will always be openings for infrastructure plumbing developers but the percentage should be lower than business application developers.

PS the terminology "infrastructure plumbing developer" and "business application developer" is I read from somewhere which I find it quite relevant also.
Last edited on
@sohguanh:
I think you have misread the thread. i dont understand why you write this stuff in here. it wont help our friend "b1gb0y2013".

however,
i doubt what you say. I have wrote all those algorithms including Red-Black Trees, AVL trees, etc.. with just stdio.h when i was learning C and algorithms.
C++ STL is maybe easy for commercial uses, which is also slower. I think its good for writing only OOP things.

my advise for "b1gb0y2013" is that if you want to learn, just try to go to the lower level. if you want to be a programmer with lots of money, you can stick with what "sohguanh"say. but remember that writing a compiler/linker/kernal is far more advanced than writing some random supermarket system. irony is that if you want money, u will have to go to the top with your advanced program, or just write another supermarket software.

i suggest that you to create your own standard libraries with all the algorithms you need and keep them. so when you need them, you dont have to ask anyone else. and also you will learn a lot.

cheers..
Hi chathura666 I post in wrong forum.

As for your statement "C++ STL is maybe easy for commercial uses, which is also slower. I think its good for writing only OOP things." it seems pretty biased. I do not want to start a flame war but if you have the chance to peek at C++ STL code by certain Open Source implementations, I doubt you would want to say it is easy at all. In fact it can be as complex as C especially C++ STL uses templates which is a new concept not available in C previously.

Finally performance is important yes but time to market is important too. If you take ages to deliver a low level C business application, you would have lose out to your competitors cuz other newer technologies have swept the scene. This is why I say C++ is as close to Java in terms of time to market although I got to admit there is still a time-line gap but it is closer than C IMHO.
yes, thats why i say. C is good for lower level things. C++ is good for business things.

i love game developing. so, when i do things, i combine all C , C++ and NASM together to write a good game. because speed matters for a game. also too much speed cam make the game hard. you have to control the speed. its a great way of learning too.

but when comes to the business application industry, people write programs with virtual machines llike in Java, ( most newer game engines also use VM architecture ) and use agile methods.

Actually its all about Project Management. you have to understand and consider the time shedule, effort, resources and all those Software Engeneering stuff and your project management experience will help you there.

well, i think we are making mistake going AWOL.. hehe... lol... anyway, its nice to share ideas sohguanh..

cheers...
Topic archived. No new replies allowed.