Hey, so I have a file I use to store my own math functions called MyMath.h. I included MyMath.h in my main.cpp file and it worked fine. But then I tried including it to another file, and I get this error:
Block.obj : error LNK2005: "int __cdecl Random(int,int,bool)" (?Random@@YAHHH_N@Z) already defined in main.obj
Why is that happening? I have the code in the header surrounded by an #ifndef, if that might be it.
Here's MyMath.h:
#ifndef MY_MATH_H
#define MY_MATH_H
int Random(int my_min = 0,int my_max = RAND_MAX,bool use_complex = false);
int Complement(int angle);
int Suplement(int angle);
double Angle(double in_angle,bool use_degrees = true);
#define PI 3.14
int Random(int my_min, int my_max,bool use_complex)
{
int result = 0;
if(use_complex == false)
{
int range = my_max-my_min;
result = rand()%(range+1) + my_min;
}
else if(use_complex == true && my_min < my_max)
{
do
{
result = rand();
}
while(result < my_min || result > my_max);
}
return result;
}
double Angle(double in_angle,bool use_degrees)
{
double max_angle = use_degrees ? 360.0 : 2.0*PI;
while(in_angle >= max_angle)
in_angle -= max_angle;
while(in_angle < 0)
in_angle += max_angle;
return in_angle;
}
int Complement(int angle)
{
angle = Angle(angle);
while(angle > 90)
angle -= 90;
return 90-angle;
}
int Suplement(int angle)
{
angle = Angle(angle);
if(angle > 180)
angle -= 180;
return 180-angle;
}
#endif
The line #include "MyMath.h"
is found in the top of both main.cpp and Block.h. Block.cpp is what was complaining.