Issue with a varible that was made

Our teacher generated a randomizer function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef RANDOMIZER_H
#define RANDOMIZER_H

void randomize(int a_seed = 0)
{

	//type to hold calender time
    time_t sysTime;
    //Structure to hold time
    struct tm timeBox;
    //Read System time
    sysTime= time(NULL);

	//Convert calender time to tm structure
    gmtime_s(&timeBox, &sysTime);
	
	//Generate the seed
    unsigned seed =  timeBox.tm_sec
                    +timeBox.tm_min
                    +timeBox.tm_hour;
    //feed the seed
    srand(seed+a_seed);
    return;
}

#endif 


however i get a c2005 error when i try to declare the function

if i declare it globally, it then says the randomize function

then it says that the following

Error 1 error C2668: 'randomize' : ambiguous call to overloaded function

what should i do to fix that

it is the last thing we need to get our game running
Any particular reason you're doing all of that instead of just calling srand(time(0));? You're actually making the seeding process less random and increasing the odds of duplicate seeds. Not to mention the code is much longer (and apparently isn't even compiling).


As for your problem... there must be some other function called randomize() somewhere that's defined globally.
it is globally defined in Globals.h

and apparently his i designed so that it isn't repeating .......

so in my globals.h

i declare void randomize();

in the cpp i am calling the function i use

randomize();

and in my library.h which stores all my header files i declare

#include "Randomizer.h"

yet i get those errors.once i fix this my game is pretty much complete
There is a randomize function defined in standard C++ (libraries), use a namespace for your function or change the name.
pathios wrote:
so in my globals.h

i declare void randomize();

That's the overloaded function. Your randomize() function takes an int as argument. It should be void randomize(int);, but I don't see why you need to forward declare it in a different header file. EDIT: although I'm not sure this is the problem, since there's nothing ambiguous about the call.

Kyon wrote:
There is a randomize function defined in standard C++ (libraries)

AFAIK, there's only rand().
Last edited on
it worked for a sec now i am getting it this

error LNK2005: "void __cdecl randomize(int)" (?randomize@@YAXH@Z) already defined in Deal.obj

however there is no reference to randomize in my current page hence why i am unsure of how it is not working now when all i did was make and adjustment that has no bearing on that particular function


like i said before the randomize(); function itself is only called for one thing specifically so how it is effecting something it isn't even mentioned in confuzzles me
+1 to filipe

The linker error sounds like you're putting the function body in the header. You can only really do this if the function is inline.

So one solution:

1
2
3
4
5
6
7
8
// in your header
void randomize(int a_seed = 0);

// in your cpp file
void randomize(int a_seed)
{
  // ... function body here
}


or

1
2
3
4
5
// in your header
inline void randomize(int a_seed = 0)  // note the inline keyword
{
  // ... function body here
}



But again note: This randomize function is ill formed. You're actually better off just calling srand(time(0));


EDIT: omg I just saw you said your teacher made that function. Aye ya. >_<
Last edited on
thanks Disch the Inline solve the problem thanks

however since i am a beginner i like understanding stuff i use.....

what exactly does inline do to the said function?
Nothing at all, it just lets you declare it in the header. Now, back in the day when compilers weren't such pros at optimizing code, the inline keyword meant that the function body was put into the place where the function call was, sort of like a macro, except that it was treated as a function.
Last edited on
Topic archived. No new replies allowed.