Problem with class.

Apr 23, 2013 at 9:35pm
I have this code where I need to add the inches and feet together to 1 number. I have to use int CDistance::add(const CDistance&) const to do it though. But when I have this I can't put d1.add() in main. Now when I take out const CDistance& total) it works. How do you do it with the whole thing in there?

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
  #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()
{
	char choice;
	CDistance d1(0, 0);

	d1.setDist();
	d1.printDist();
	d1.add();
	_getch();
	return 0;
}
Apr 23, 2013 at 9:55pm
You declared member function add as having one parameter but you are trying to call it without any argument. So the compiler issues an error because the number of arguments does not correspond to the number of parameters.
Last edited on Apr 23, 2013 at 9:56pm
Apr 23, 2013 at 10:00pm
how would I fix this?
Apr 23, 2013 at 10:21pm
The number of arguments shall correspond to the number of parameters. Is it clear?
Apr 23, 2013 at 10:25pm
no im completely lost sorry
Apr 23, 2013 at 11:53pm
Topic archived. No new replies allowed.