error LNK2005, i need help for that??

Hi everyone,

I wrote my codes in my program, but i got this error every time when i compile my program<<

1
2
3
Error	1	error LNK2005: "public: void __thiscall MyFloat::Write(void)" (?Write@MyFloat@@QAEXXZ) already defined in MyFloat.obj	C:

Error	2	error LNK1169: one or more multiply defined symbols found	C:


here my program

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 #include <iostream>
using namespace std;
/*

Action: This assignment cells for doing a partial implementation of the abstract
data type "MyFloat". This ADT is to serve a specialized need for small floats
between 0 and 1 that can have up to 20 digits of accuracy. The creation of
the MyFloat class will allow calculations with with accuracy not possible with floats.
*/
class MyFloat{

private:

	enum { MAX_DIGITS = 20 };
	char Number[MAX_DIGITS + 1];
	char NumberOfDigits;

public:

	MyFloat()
	{
		NumberOfDigits = 0;
	}

	void Write();
	//int read();

	friend void AssignValue(MyFloat& X);
};


void MyFloat::Write(){

	cout << "0.";

	if (NumberOfDigits != 0)
	for (int i = 1; i <= NumberOfDigits; i++)
	{
		cout << int(Number[i]);

	}
	else

		cout << "?";

}





i want to link my program or codes to this program

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
#include <iostream>
#include "MyFloat.cpp"
using namespace std;

void AssignValue(MyFloat& X)
{
	X.Number[1] = 1;
	X.Number[2] = 2;
	X.Number[3] = 3;          // run program first this way with
	X.NumberOfDigits = 0;     // these numbers then change
}                           // X.NumberofDigits = 0, to test
// "0.?", which stands for an error

void main()
{
	MyFloat X;

	AssignValue(X);

	cout << "X = ";
	X.Write();
	cout << endl << "Press Enter key to continue";
	cin.ignore();
}


everything is good so far and my codes has no error, but i think only problem i got is linking between those programs.

Thanks and sorry for my bad English.
Last edited on
You should never #include source files, they should be added to your project.

Also #include files should not contain executable code, except when dealing with templates.

Thank you, but can you explain how does this work?
because i tried this before and does not work.

Finally, i solved my problem. i just change header and compiler from setting, my project make it as header and another or second one make is as compiler. finally, works fine.

Thanks :)
Topic archived. No new replies allowed.