Srand Problems

Started by
2 comments, last by doho 19 years, 3 months ago
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 
						// In the header file.


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?
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
Advertisement
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.
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 :)
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
Quote:Original post by Surg AKA Kunark
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 :)


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.

This topic is closed to new replies.

Advertisement