Why am i getting this error?



inc13.obj : error LNK2019: unresolved external symbol "public: __thiscall CDistance::CDistance(void)" (??0CDistance@@QAE@XZ) referenced in function _main

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
  #include <iostream>
#include <conio.h>
using namespace std;

class CDistance
{
private:
		int feet, inches;
public:
		CDistance();
		CDistance(int, int);
		~CDistance();
		void setDist();
		void printDist() const;
		int add(const CDistance&) const;
		int subtract(const CDistance&) const;
};

CDistance::CDistance(int f, int i)
{
	feet = f;
	inches = i;
}

CDistance::~CDistance() {}

void CDistance::setDist()
{
	cout << "Enter the distance. Feet then inches: ";
	cin >> feet >> inches;
}

void CDistance::printDist() const
{
	cout << "Feet: " << feet 
		 << "Inches: " << inches << endl;
}

int CDistance::add(const CDistance& total) const
{
	cout << feet + inches;
	return 0;
}

int CDistance::subtract(const CDistance& total) const
{
	cout << feet - inches;
	return 0;
}


int main()
{
	CDistance d1;

	d1.setDist();
	d1.printDist();
	_getch();
	return 0;
}
because you declared it in the class constructor but never actually defined it :)
I actually never defined what?
In main() - Look at the definition of the object being created. Try adding some arguments to that (look at the signature of the constructor)

1
2
3
4
5
CDistance(int, int);

// so use in main --->

CDistance d1(1,2);


Also if you want to overload the constructor, you must define both. You have only defined CDistance::CDistance(int f, int i) but declared and have not defined CDistance::CDistance();

1
2
3
4
5
6
7
8
9
10
CDistance::CDistance() {
	feet = 0;
	inches = 0;
}

CDistance::CDistance(int f, int i)
{
	feet = f;
	inches = i;
}
Last edited on
I put this and now it runs. Thanks!

CDistance d1(0,0);
Topic archived. No new replies allowed.