Linker Errors - In Bulks.

Hello.

I've been continuing trying to improve my little "calculator". It's just a basic one with addition, substraction, multiplication and division.
So far, I managed to add Addition and Substraction into one program.

Then I tried with Multiplication and Division, but the turnout weren't quite as good, as with the first one I did.

When I click compile, it gives me a lot of Linker Errors.

If anyone can help me on this one, it'd be greatly appreciated!

The program's 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
int addition(int x, int y);#include <conio.h>
#include <iostream>
#pragma hdrstop

using namespace std;

int addition (int, int);
void showResult(int);

int main(int argc, char **argv, int res) 
//    int main (int, char**)



      {
          int x, y, result;
char calcer;
    cout << endl <<"Enter the first value:";
    cin >> x;
    cout << endl <<"Enter your calctype\n + for Addition, - for Subtraction\n* for multiplication and / for division:";
    cin >> calcer;  
    cout <<"Enter the second value: ";
    cin >> y;

    int addition (int, int);
                   if (calcer == '+'){
                result = addition(x, y);                
                showResult(result);
                                     }
    int subtract (int, int);
    if (calcer == '-') {
                result = subtract(x, y);
    showResult(result);
    
                       }

int multiplication (int, int);
           if (calcer == '*'){
                result = multiplication(x, y);                
                showResult(result);
                             }
int dividation (int, int);
           if (calcer == '/'){
                result = dividation(x, y);                
                showResult(result);
                             }
else {
     cout<<"Incorrect calculation type selected. Please restart the program.\n";
     } 
     
    {
    cout << endl << endl << "Press any key to continue...";
    getch();
    return 0;

    }
int addition(int x, int y);
     {
     return x + y;   //change to + - * or /.
     }

int subtract(int x, int y);
     {
     return x - y;   //change to + - * or /.
     }

int multiplication(int x, int y);
     {
     return x * y;   //change to + - * or /.
     }

int dividation(int x, int y);
     {
     return x / y;   //change to + - * or /.
     }

void showResult(int res);

        { 
        cout<<"The result is "<< res << endl;
        }
}


The Linker Errors are following:

[Linker error] undefined reference to `addition(int, int)'
[Linker error] undefined refence to `showResult(int)'
-----------.------------------------- subtract(int, int)
-----------.------------------------- showResult(int)
-----------.------------------------- multiplication(int, int)
-----------.------------------------- showResult(int)
-----------.------------------------- dividation(int, int)
-----------.------------------------- showResult(int)

Thanks.
Last edited on
I found a number of problems...first, you are defining your functions incorrectly. You do not want the ; at the end of your function definitions:

i.e.
1
2
3
int addition(int x, int y) {
     return x + y;
}


Second, you want to place all those function definitions at the beginning of the code or prototype them:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//prototyping
int addition(int x, int y);
//other funcs

int main() {
     //...
}

int addition(int x, int y) {
     return x + y;
}
//etc

//-----------------------------------

//the other way
int addition(int x, int y) {
     return x + y;
}
//other definitions

int main() {
     //...
}


Finally, you do not want to define functions in main(), you should move them outside of it as shown above.
Hello. I did what you said, but this results in either the program not working properly (Not being able to calculate), or just a plain error when compiling.
Hm, then rearrange it so it looks like this...it _should_ work like this, but I haven't tested it:

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
int addition(int x, int y) {
     return x + y;   //change to + - * or /.
}

int subtract(int x, int y) {
     return x - y;   //change to + - * or /.
}

int multiplication(int x, int y) {
     return x * y;   //change to + - * or /.
}

int dividation(int x, int y) {
     return x / y;   //change to + - * or /.
}

void showResult(int res) { 
     cout<<"The result is "<< res << endl;
}


int main(int argc, char **argv, int res) {  //<-- what are these for? it doesn't seem like you use them
     int x, y, result;
     char calcer;
     cout << endl <<"Enter the first value:";
     cin >> x;
     cout << endl <<"Enter your calctype\n + for Addition, - for Subtraction\n*     fpr multiplication and / for division:";
     cin >> calcer;  
     cout <<"Enter the second value: ";
     cin >> y;
     if (calcer == '+') {
           result = addition(x, y);
           showResult(result);
     } else if (calcer == '-') {
           result = subtraction(x, y);
           showResult(result);
     } else if (calcer == '*') {
           result = multiplication(x, y);
           showResult(result);
     } else if (calcer == '/') {
           result = dividation(x, y);
           showResult(result);
     } else {
         cout<<"Incorrect calculation type selected. Please restart the program.\n";
     } 
     cout << endl << endl << "Press any key to continue...";
     getch();
     return 0;
}
Last edited on
That worked perfectly!
The program runs without problems now.
Thank you very much for your help! ;)
Topic archived. No new replies allowed.