Hi, I'm trying to make myself a file where I declare all my functions, but it isn't going well. On my first function I get an error:
Multiple markers at this line
- first defined here
- multiple definition of `random(double)'
the code of funct.h:
1 2 3 4 5 6
#include <stdlib.h>
double random (double x)
{ //the error's at this line
return (double) ( (rand () / RAND_MAX) * x);
}
and the main.cpp is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <windows.h>
#include "funct.h"
usingnamespace std;
double a, b = 15.367;
int main ()
{
a = random (b);
cout << a << endl;
system ("PAUSE");
return 0;
}
You don't have ANY other files? I'm doubtful of that.
Anyway.. try making the function inline:
inlinedouble random(double x)
Or just put the prototype in the header and put the function body in a seperate cpp file.
Also your random function is wrong. rand() / RAND_MAX will always be zero because it's integral division. You'd need to cast to floating point to get floating point division.
Also your random function is wrong. rand() / RAND_MAX will always be zero because it's integral division. You'd need to cast to floating point to get floating point division.
I've played around a bit, and I found out that if I put rand (); right after srand (time (0)); every next time rand (); works perfectly... ... uh.. could someone explain that
srand initializes the random number seed and is needed for rand(the random number generator) to generate a different random number. The srand command should only be used once ,(before the first rand statement is implemented) and will accomodate all subsequent occurances of rand statement in that function.