Question: Operators overloading

Hi everyone.. I'm having some troubles with operators overloading....
I wrote a point class (fPoint):
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
#include "stdafx.h"

#include <iostream>
using namespace std;

class fPoint
{
public:
	float x,y;

	//Main Constructor
	fPoint(float X,float Y)
	{
		x=X;
		y=Y;
	}
	
	//Default Constructor
	fPoint()
	{
		x=0;
		y=0;
	}
	friend fPoint operator+(fPoint p1,fPoint p2);
};// end of class

fPoint operator+(fPoint p1,fPoint p2)
{
	return fPoint(p1.x+p2.x,p1.y+p2.y);
}


The problem is: the overloaded (+) works fine IF the previous code is in the same cpp file that contains the main() function
but when I put that code in any other file (eg. 1.cpp) and write the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
// MyTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "1.cpp"


int main()
{
	fPoint p1(1,3) , p2(3,4);
	fPoint p3=p1+p2;

	return 0;
}


two errors pop up!
1
2
Error	1	error LNK2005: "class fPoint __cdecl operator+(class fPoint,class fPoint)" (??H@YA?AVfPoint@@V0@0@Z) already defined in MyTest.obj	1.obj	MyTest
Error	2	fatal error LNK1169: one or more multiply defined symbols found	C:\CppPros\MyTest\Debug\MyTest.exe	1	MyTest


(I'm using Microsoft Visual Studio 2008 professional edition)

any ideas???
Thanks...
Last edited on
Create a header file and place the class declaration in there and then include the header file
Last edited on
Topic archived. No new replies allowed.