Linker Error- what is wrong ?

i get this linker error after defining the following member functions and calling one function that calls the others to perform small arithmetic calculations. What could be wrong here ?:

Error 1 error LNK2005: "public: int __thiscall KRA::calc_balance(int &,int &)" (?calc_balance@KRA@@QAEHAAH0@Z) already defined in main.obj



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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126



#include <string>
#include <iostream>
#include <fstream>
using namespace std;

class KRA; //declaring a class
void receive_stamp_duty (); //function prototype whose definition is below 

int main ()
{

receive_stamp_duty ();
return 0;

}

class KRA 
	{

private:
int stamp_duty_received, Land_value,stamp_duty_amount, balance;
string Surname, OtherNames, LandNum, KraPin, OwnerIdCard;
	
public:
KRA (){}      //default constructor		
KRA (string a, string b, string c,string d,string e) //overloaded constructor
		{
		a =  Surname;
		b=OtherNames;
		c= LandNum;
		d=KraPin;
		e=OwnerIdCard;
		}

	void setLandvalue (int value)    //setter function
		{
		Land_value =value;
		}

		void setReceivedMoney (int received )    //setter function

		{
		 stamp_duty_received = received;
		}
		

	 int calc_stamp_duty_amount (int &Land_value);   //function defined out of class as below
		
	 int calc_balance  (int&, int&);     //function defined out of class as below

		
		
void receive_stamp_duty ();
static const string transact;   //a default string-no longer in use

	};
//const string KRA:: transactionState ="cleared";  
//calc balance, stamp_duty_amount (a 15% of Land value)

			
		int KRA:: calc_stamp_duty_amount (int &Land_value)
		{
		stamp_duty_amount = 15% Land_value;
		return stamp_duty_amount ;
		}

		int KRA::calc_balance  (int &stamp_duty_received, int &stamp_duty_amount)
		{
			balance= stamp_duty_amount  - stamp_duty_received;
		return balance;
		}

void KRA :: receive_stamp_duty ()

{

	std::cout <<"Enter the value of Land on Sale\n";

cin>>Land_value;
cin.clear ();
	


	std::cout<<"You should pay stamp duty amount :\n"<<calc_stamp_duty_amount (Land_value)<<endl;
	
	std::cout<<"Enter the amount received in respect of land transaction stamp duty:\n"; 

	std::cin>>stamp_duty_received;
	 
	setReceivedMoney(stamp_duty_received);



if (stamp_duty_received	==stamp_duty_amount) 
{
	std::cout<<"Transaction stamp duty has been paid in full\n"<<endl; 

	ofstream  enter ("Transactions.txt", ios::app);

/*
executes some more code here

*/
	enter.close();

}

if (stamp_duty_received	< stamp_duty_amount)
	
{

	
std::cout<<"Stamp duty fee should be: \n"<<stamp_duty_amount<<" .Please pay ksh.\n" <<calc_balance (stamp_duty_received, stamp_duty_amount)<<"more !"<<endl;
		
	
ofstream  enter ("Transactions.txt", ios::app);
/*
executes some more code here

*/
enter.close();
}
}
Last edited on
http://www.cplusplus.com/forum/general/140198/


you need to provide a testcase that does reproduce your issue.
if you have several files, upload to github or similar.
There are a few other errors as well that seem related:

98:1: warning: "/*" within comment [-Wcomment]
110:1: warning: "/*" within comment [-Wcomment]
113:1: warning: "/*" within comment [-Wcomment]
20:13: error: expected ')' before 'a'
48:14: error: 'string' does not name a type
In member function 'void KRA::receive_stamp_duty()':
70:2: error: 'cout' is not a member of 'std'
72:1: error: 'cin' was not declared in this scope
79:2: error: 'cout' is not a member of 'std'
79:72: error: 'endl' was not declared in this scope
81:2: error: 'cout' is not a member of 'std'
83:2: error: 'cin' is not a member of 'std'
85:38: error: 'setREceivedMoney' was not declared in this scope
91:2: error: 'cout' is not a member of 'std'
93:2: error: 'ofstream' was not declared in this scope
93:48: error: expected '}' at end of input
87:6: warning: unused variable 'balance' [-Wunused-variable]
93:48: error: expected '}' at end of input
> that seem related:
those are compiler errors, the one in the OP is a linker error, meaning that it has passed compilation stage. So the testcase is useless.
I have included the headers i had omitted plus some variables i hadn't declared (kindly run the above code).

The following linker errors remain.

1
2
3
4

1>SecondTry.obj : error LNK2028: unresolved token (0A000305) "void __cdecl receive_stamp_duty(void)" (?receive_stamp_duty@@$$FYAXXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>SecondTry.obj : error LNK2019: unresolved external symbol "void __cdecl receive_stamp_duty(void)" (?receive_stamp_duty@@$$FYAXXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>C:\Users\dell\Documents\Visual Studio 2005\Projects\ErrorTry\Debug\ErrorTry.exe : fatal error LNK1120: 2 unresolved externals
Last edited on
http://www.cplusplus.com/forum/general/113904/#msg622050

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void receive_stamp_duty (); //declaration of a global function

int main ()
{
   receive_stamp_duty (); //calling the global function
   return 0;
}

class KRA{
//...
   void receive_stamp_duty (); //declaration of a member function
};

void KRA :: receive_stamp_duty () //definition of a member function
{
   //...
}
in main() you try to call a global non-member function (note that you are not using any objects), however you have defined a member function.
$ nm -C foo.o | grep receive_stamp_duty
                 U receive_stamp_duty()
0000000000000068 T KRA::receive_stamp_duty()
see that it can't find the definition for the non-member funtion, you it does find the one in KRA.


PS: learn to indent.
closed account (48T7M4Gy)
Admittedly there are bigger fish to fry, but put these problems need to go on the ToDo list:

1
2
3
4
5
6
7
8
KRA (string a, string b, string c,string d,string e) //overloaded constructor
		{
		a =  Surname;
		b=OtherNames;
		c= LandNum;
		d=KraPin;
		e=OwnerIdCard;
		}

Single character variables are poor coding practice. The assignments are around the wrong way given the (private) members.

And is line 66 really meant to be a modular arithmetic exercise?
Last edited on
Topic archived. No new replies allowed.