Jump to content
  • Advertisement
Sign in to follow this  
KodeNerd

Global function Linking

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

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?

Share this post


Link to post
Share on other sites
Advertisement
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)));
}




Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Yes that worked. I knew I was missing something stupid (I normally put the definition in a .cpp but didn't for some reason).

Share this post


Link to post
Share on other sites
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)));
}

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!