Public Group

# Srand Problems

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

## Recommended Posts

Okay, I thought I had did this all right, but I guess not... I've created myself a header file. That does a few simple things, has a randomize function in it that produces random numbers, a createdeck function that initializes A deck with a struct called SCardDeck, populating the array with the correct value, and then has a shuffledeck function that shuffles this deck that we have created. Now Im not sure why, but for some reason when I call srand(time(0)); in the header file it gives me an error, the error is c:\Documents and Settings\Administrator\Desktop\Johnathan\Include\NumberManip.h(20): error C2501: 'srand' : missing storage-class or type specifiers c:\Documents and Settings\Administrator\Desktop\Johnathan\Include\NumberManip.h(20): error C2365: 'srand' : redefinition; previous definition was a 'function'
srand(time(0));			// This will be called once, to seed our random numbers
// Should only be called once, that's why I included it


Is there any way I can fix this, or do I have to take srand out of the header file and place it in my main?

##### Share on other sites
Put your function in a c/cpp file instead and only put its declaration in the header file.

// utils.c#include <stdlib.h>void do_something(){  // put your srand call here}// utils.hvoid do_something();// main.cinclude "utils.h"void main(){  do_something();}

Only put the stuff in the header files that letterly has to to be there.

##### Share on other sites
Thank you for the help, but I was actually able to figure out how to fix it, I just needed to create a variable that checked if I had seeded the function, if not when I called my random function it would seed it, soon as it was seeded once it would save that, and then it wouldn't be seeded again.

THe reason I have this in a header file, is because I may need the random functon on many programs, like I already have. And Im going to try to make a couple card games which I will need all these functions for as well, so If i put them all in one header file, Im able to include the actual functions in every cpp file i make. Instead of having to retype everything out again.

Again thank you for the help :)

##### Share on other sites
Quote:
 Original post by Surg AKA KunarkThank you for the help, but I was actually able to figure out how to fix it, I just needed to create a variable that checked if I had seeded the function, if not when I called my random function it would seed it, soon as it was seeded once it would save that, and then it wouldn't be seeded again.THe reason I have this in a header file, is because I may need the random functon on many programs, like I already have. And Im going to try to make a couple card games which I will need all these functions for as well, so If i put them all in one header file, Im able to include the actual functions in every cpp file i make. Instead of having to retype everything out again.Again thank you for the help :)

Again, only put stuff in header files that really has to be there. Inline functions used by multiple source files are one example.

You can always make a "source and header file couple" with the code that you want to share. Then you only have to copy them over to your new project, and you don't need to retype anything.

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633675
• Total Posts
3013278
×