Error LNK2019: Unresolved external symbol

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
class NumDays
{
	private:
		double hours;
		double days;
	public:
		//constructor that assigns values to both hours and days, but only
		//takes in the number of hours as its argument
		NumDays(double h = 0)
			{ setHours(h);
			  setDays(h / 8.0);}
	
		// set function that assigns values to both hours and days, but only
		//takes in the number of hours as its argument
		void setHours(double h)
			{ hours = h;
			  days = h / 8.0; }
	
		double getHours() const
			{ return hours; }

		// set function that assigns values to both hours and days, but only
		//takes in the number of days as its argument
		void setDays(double d)
			{ days = d; 
			  hours = d * 8.0; }
	
		double getDays() const
			{ return days; }
	
		NumDays operator + (NumDays &);
		NumDays operator ++ (); // Prefix ++
		NumDays operator ++ (int); // Postfix ++
};


int main()
{
	NumDays dayOne(17), dayTwo(27), dayThree, dayFour, dayFive;

	cout << "Day One: " << dayOne.getDays() << endl;
	cout << "Day Two: " << dayTwo.getDays() << endl;

	// Overloaded Add
	dayThree = dayOne + dayTwo;
	
	// Display Day three.
	cout << "Day Three (in days): " << dayThree.getDays() << endl;
	cout << "Day Three (in hours): " << dayThree.getHours() << endl;
	cout << endl;
	
	// Overloaded Postfix increment
	dayFour = dayThree++;
	cout << "Day Three (in days): " << dayThree.getDays() << endl;
	cout << "Day Three (in hours): " << dayThree.getHours() << endl;
	cout << "Day Four (in days): " << dayFour.getDays() << endl;
	cout << "Day Four (in hours): " << dayFour.getHours() << endl;
	cout << endl;
	
	// Overloaded Prefix increment
	dayFive = ++dayThree;
	cout << "Day Three (in days): " << dayThree.getDays() << endl;
	cout << "Day Three (in hours): " << dayThree.getHours() << endl;
	cout << "Day Five (in days): " << dayFive.getDays() << endl;
	cout << "Day Five (in hours): " << dayFive.getHours() << endl;
	
	system("pause");
	return 0;
}



With the above code I am getting the following errors;

: error LNK2019: unresolved external symbol "public: class NumDays __thiscall NumDays::operator++(void)" (??ENumDays@@QAE?AV0@XZ) referenced in function _main
: error LNK2019: unresolved external symbol "public: class NumDays __thiscall NumDays::operator++(int)" (??ENumDays@@QAE?AV0@H@Z) referenced in function _main
: error LNK2019: unresolved external symbol "public: class NumDays __thiscall NumDays::operator+(class NumDays &)" (??HNumDays@@QAE?AV0@AAV0@@Z) referenced in function _main



I'd assume that fixing one error will fix the rest of them, but I've been at this for a while now and cannot figure out what is wrong. Any help is greatly appriciated.
dayFour = dayThree++; calls NumDays::operator++(int)
dayFive = ++dayThree; calls NumDays::operator++()
dayThree = dayOne + dayTwo;; calls NumDays::operator+(const NumDays&)
... none of which you have defined.

I'm not sure I follow.
1
2
3
NumDays operator + (NumDays &);
NumDays operator ++ (); // Prefix ++
NumDays operator ++ (int); // Postfix ++ 


None of these are defined anywhere.
You just declared the overloading, but didn't define their implementation.

Take "NumDays operator + (NumDays &);" as an example, the following should work.

"
NumDays operator + (NumDays &Days); //I use it to replay your NumDays operator + (NumDays &)

"

// Add the following after the NumDays class declaration
NumDays NumDays::operator+(NumDays &Days)
{

return NumDays(this->hours + Days.hours);

}
"
In this way, the compiler has been exactly told how to implement "+" overloading.

Hopefully it can help you.


b2ee
Okay, so after the NumDays class declaration I should have one prototype for the three operator functions I have? Like the following; ?

1
2
3
4
5
6
7
8
9
10
NumDays NumDays::operator+(NumDays &Days)
{ return NumDays(this->hours + Days.hours); }

NumDays NumDays::operator ++ ()// Prefix ++
{ ++hours;
  return NumDays(); }

NumDays NumDays::operator ++ (int) // Postfix ++
{ hours++;
  return NumDays(hours); }
Last edited on
Try to run them and take a look whether the result is what you expect.

I guess there is some wording error in

"
NumDays NumDays::operator ++ ()// Prefix ++
{ ++hours;
return NumDays(); } // is "return NumDays(hours);" more like what you want?

"

b2ee
Topic archived. No new replies allowed.