Skip to main content
GameDev.net gamedev.net
🔒 Locked

Global function Linking

Started by KodeNerd Jan 12, 2008 at 11:29 PM 4 replies 1k views
Original Post
KodeNerd
KodeNerd
When I add this:

#ifndef SEVN_RANDOM
#define SEVN_RANDOM
int Random(int low=0,int high=RAND_MAX) {
	return ((rand()%high+(++low)));
}
#endif

to a .h file and include it (more than once) I always get a multiple definition error when I link...it should not do that. Is there something that I missed?
------------George Gough
etsuja
etsuja
you're defining the whole definition in the header which won't work...

put this in the header...

int Random(int low=0,int high=RAND_MAX);


and this in the source code.

int Random(int low=0,int high=RAND_MAX) {	return ((rand()%high+(++low)));}


Artist 1st - Programmer 2nd(I'll get some material linked here sometime to support these claims, haha)
ender7771
ender7771
In practice, one should only put a function definition, like

"int Random(int, int);" in a .h, and then the actual code in a .cpp. Try that, and it might fix your problem.
KodeNerd
KodeNerd
Yes that worked. I knew I was missing something stupid (I normally put the definition in a .cpp but didn't for some reason).
------------George Gough
fpsgamer
fpsgamer
Quote:
Original post by KodeNerd
When I add this:
*** Source Snippet Removed ***
to a .h file and include it (more than once) I always get a multiple definition error when I link...it should not do that. Is there something that I missed?


If you wish you define a function within a header file it must be declared inline.

inline int Random(int low=0,int high=RAND_MAX) {	return ((rand()%high+(++low)));}
KodeNerd
KodeNerd
That is something I did not know. Thanks.
------------George Gough

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.