Problem with function overloading.

Hello. I have made a .h file called "Vector.h" in order to store some of the commonly used operations I perform on vectors, as they don't seem to automatically be included. Here is a segment of the code:

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
27
28
#ifndef VECTOR_H
#define VECTOR_H

//This extends the vectors functions

#include <d3d9.h>
#include <d3dx9.h>
#include <math.h>

//Make a 3D version of POINT
struct POINT3 : POINT
{
	float z;
};

float length(D3DXVECTOR2 v)
{
	return sqrt( pow(v.x, 2) + pow(v.y, 2) );
}

float length(D3DXVECTOR3 v)
{
	return sqrt( pow(v.x, 2) + pow(v.y, 2) + pow(v.z, 2) );
}

//...more after this point

#endif 


Basically, each function is duplicated for a 2D and 3D DirectX vector. Now, up until recently I only #include-ed this file in a .cpp file for a class. However, I now find that I need that classes header file to include it too:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef PLAYER_H
#define PLAYER_H

#include <vector>
#include "../../Camera.h"
#include "../../Overlay.h"
#include "../../Define.h"
#include "../../Vector.h" //Include added vector functionality

//Temp
#include "../../Model.h"

namespace EXAMPLE
{
	class Player
	{
               private:
               POINT3 pt;
               //Etc.
        };
}


Upon including that file however, Visual Studio 2010 refused to compile, giving me the following errors:

1>main.obj : error LNK2005: "float __cdecl length(struct D3DXVECTOR2)" (?length@@YAMUD3DXVECTOR2@@@Z) already defined in Player.obj
1>main.obj : error LNK2005: "float __cdecl length(struct D3DXVECTOR3)" (?length@@YAMUD3DXVECTOR3@@@Z) already defined in Player.obj


If I include it in Player.cpp it does not give me these compiling errors. But if I include it in Player.h it does. For the sake of it, here is Player.cpp:

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
27
28
29
30
31
#include "Player.h"
#include "../../Control.h"

#define DIST 300	//How far we can "see" ahead of us. Set it back to 200 once you're done.
#define VIEWT 33	//Angle +/- for top view... May need tweaking.
#define VIEWS 22.5	//Angle +/- for side view
#define PREC  0.001  //Precision for which to check the triangle
#define PI 3.14159265

namespace EXAMPLE
{
	Player::Player(Camera* _camera, Overlay* _overlay)
	{
		camera = _camera;
		overlay = _overlay;

		modSphere =	     new Model("data/example/model/mod_debug.x",
					     new Texture("data/example/texture/tex_room.png" ) );

		TEXTASSIGN(text);

		camera->setY(-5);
		//camera->setZ(10.0);

		x = camera->getX();
		y = camera->getZ();
		z = -camera->getY();
	}

        //etc.
}


Any help would be appreciated. I just can't figure out where to even start with this problem.
The errors you're seeing are not a compile error, but rather a linker error.
The linker is telling you that it found the same functions (length) multiple times.
Once in main.obj and once in player.obj.
The linker considers this an error since it doesn't know that they are the same.

This occured because you defined the two length functions in a .h file that was included in both main.cpp and player.cpp, therefore the functions were compiled twice.

It's not a good practice to define functions (or variables) in multiple places.
There is a linker switch that can tell the linker to ignore such errors, but I don't recommend that. Your best course of action is to put the two length functions in their own .cpp file.
Hooray! Thanks! By placing all the function definitions in a separate .cpp file, the program both compiled again, and its code looks much neater. I'll keep this in mind from now on, thanks!
Topic archived. No new replies allowed.