Why will this not run?



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
  #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;
}

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;
}

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

int main()
{
	CDistance d1;

	d1.setDist();
	d1.printDist();
	_getch();
	return 0;
}
Add and subtract has a non-void return type but doesn't return anything.

You have not defined the default constructor and the destructor.
I'm still confused
Add and subtract has return type int. You are not returning an int (or anything) from these functions. If you want them to return int you will have to add a return statement (e.g. return 1;). If you don't want them to return anything change the return type to void.

The default constructor is the constructor with no parameters. You have not defined the default constructor and you have not defined the destructor.
1
2
3
4
5
6
7
8
9
CDistance::CDistance()
{
	// put code here.
}

CDistance::~CDistance()
{
	// put code here.
}
Im looking at an example my teacher has and mine is very similar and hers runs. I keep getting this error

1>inc13.obj : error LNK2019: unresolved external symbol "public: __thiscall CDistance::~CDistance(void)" (??1CDistance@@QAE@XZ) referenced in function _main
1>inc13.obj : error LNK2019: unresolved external symbol "public: __thiscall CDistance::CDistance(void)" (??0CDistance@@QAE@XZ) referenced in function _main
Well by adding this:

CDistance::~CDistance() {}

it got rid of 1 error but there is still one
1
2
3
4
5
6
7
8
9
10
11
int CDistance::add(const CDistance& total) const
{
	cout << feet + inches;
        return feet + inches
}

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


or

1
2
3
4
5
6
7
8
9
void CDistance::add(const CDistance& total) const
{
	cout << feet + inches;
}

void CDistance::subtract(const CDistance& total) const
{
	cout << feet - inches;
}
Last edited on
Topic archived. No new replies allowed.