help with classes

If i am compiling the following code, I am getting an error:

CODE:
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
#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

class length{
                int m, cm;
            public:
                //int tot_len;
                void get_length();
                void show_length();
                length add_length(length);
            };

void length::get_length()
{
    cout << "\nEnter metres(m): ";
    cin >> m;
    cout<< "Enter centimetres(cm): ";
    cin >> cm;
    cout << "Press any key to continue...";
    getchar();
}

void length::show_length()
{
    cout << endl << m << "m, " << cm << "cm" << endl;
    cout << "Press any key to continue...";
    getchar();
}

length length::add_length(length L2)
{
    length L_tot;
    L_tot.cm = (cm + L2.cm) % 100;
    L_tot.m = m + L2.m + (m + L2.m) / 100;
    return L_tot;
}

int main(void)
{
    system("cls");
    length l1, l2, l_tot;
    l1.get_length();
    l2.get_length();
    l1.show_length();
    l2.show_length();
    l_tot = add_length(l2);
    l_tot.show_length();
    getchar();
    return 0;
}


ERROR:

error: 'add_length()' was not declared in this scope


Please tell me what's the error.
Thank You.
Compare how you called add_length and the other member functions. With the others, you correctly call them with their respective objects. The same is not rue on line 49.
1
2
3
l_tot = add_length(12);
//Should be
l_tot.add_length(12);


You can either change your add_length function to modify the member variables to reflect the change or make it a friend, non-member function that takes a parameter of type length.
Topic archived. No new replies allowed.