This topic is 3908 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

#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 on other sites
you're defining the whole definition in the header which won't work...

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 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 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 on other sites
Quote:
 Original post by KodeNerdWhen 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 on other sites
That is something I did not know. Thanks.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
13

• 14
• 10
• 25
• 9
• 57
• ### Forum Statistics

• Total Topics
632641
• Total Posts
3007619
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!