Sign in to follow this  

Srand Problems

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

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?

Share this post


Link to post
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.h
void do_something();

// main.c
include "utils.h"

void main()
{
do_something();
}




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

Share this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites

This topic is 4714 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this