[Linker error] undefined reference to 'function'

Mar 31, 2013 at 6:05pm
I've been struggling with this for the last two hours, not sure what I'm missing. I've searched the forums and used Google, but can't find an answer that I understand. I'm getting the following error messages when compiling:

[Linker error] undefined reference to `getScore(float&, int&)'
[Linker error] undefined reference to `calcAverage(float, int)'
[Linker error] undefined reference to `findLowest(float, int)'
ld returned 1 exit status

We've just learned about functions in class, so I'm sure I've made a few mistakes. Some of the code for the functions are incomplete, as well, because I can't get beyond this.

Any help would be appreciated.

Here's my 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <conio.h>
using namespace std;

void getScore(float &, int &);
void calcAverage(float, int);
float findLowest(float, int);

int main()
{    
     int flag = 0;
     
     do {
     
     int size = 0;
     float grade[size];
     
     getScore(grade[size], size);
     
     calcAverage(grade[size], size);
    
     cout << "\n\nTry again?" << endl;
     cout << "Enter 1 for yes or 0 for no!" << endl << endl;
     cout << "Selection:  ";
     cin >> flag;
    
     } while (flag != 0);
    
     if (flag >= 1)
     {
         system("CLS");
     }
     else
     {
         system("CLS");
         cout << "Program shutting down..." << endl;
     }
    
     cout << endl;

	 system("pause");
	 return 0;
}


void getScore(float grade[], int &size)
{     
      int flag;
      
      do {
         
      for (;;)
      {    
         system("CLS");
         cout << "Enter a grade to be averaged or input -99 to finish:  " << endl << endl;
         cout << "Grade:  " << endl << endl;
        
              if (cin >> grade[size])
              {
                      break;
              }
                          else
                          {
                         
                          system("CLS");
                         
                          cout << "**Please enter a valid grade or -99 to finish!\n" << endl;
                          cin.clear();
                                     
                          std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                          }
    } 
      size++;
    } while (flag != -99);
}

void calcAverage(float grade[], int size)
{
     float least;
     float sum;
     float avg;
     
     least = findLowest(grade[size], size);
}

float findLowest(float grade[], int size)
{
     int counter = 0;
     float least = 100;
     
     for(counter = 0; counter < size; counter++)
     {
                 if(grade[counter] < least)
                 {
                     least = grade[counter];
                 }
     }                                              
         
     return least;
}
Mar 31, 2013 at 6:26pm
You need to correctly specify the parameters.

1
2
3
void getScore(float *, int &); // grade[], the parameter, is an array, which is a pointer
void calcAverage(float *, int); // same as above
float findLowest(float *, int); // yet again, same as above 
Mar 31, 2013 at 6:29pm
The function prototype, (lines 5-7), function definition (lines 46, 77 and 86) must agree. Also the calls to these functions must be consistent with the above.

A zero-length array is not permitted, and the size must be constant (lines 15 and 16).

Here's a partial fix, though there are still warnings about uninitialised variables which must still be corrected.

The use of size in function getscore looks very questionable, since the value passed is a constant. I'd recommend a different variable is used for this purpose.
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <cstdlib>
#include <limits>
#include <conio.h>
using namespace std;

void getScore(float grade[], int size);
void calcAverage(float grade[], int size);
float findLowest(float grade[], int size);


int main()
{    
    int flag = 0;
     
    do {
     
        const int size = 1000;  // size cannot be ZERO 
        float grade[size];      // size must be constant
     
        getScore(grade, size);
     
        calcAverage(grade, size);
    
        cout << "\n\nTry again?" << endl;
        cout << "Enter 1 for yes or 0 for no!" << endl << endl;
        cout << "Selection:  ";
        cin >> flag;
    
    } while (flag != 0);
    
    if (flag >= 1)
    {
        system("CLS");
    }
    else
    {
        system("CLS");
        cout << "Program shutting down..." << endl;
    }
    
    cout << endl;

    system("pause");
    return 0;
}


void getScore(float grade[], int size)
{     
    int flag;
      
    do {
         
        for (;;)
        {    
            system("CLS");
            cout << "Enter a grade to be averaged or input -99 to finish:  " << endl << endl;
            cout << "Grade:  " << endl << endl;
        
            if (cin >> grade[size])
            {
                break;
            }
            else
            {                     
                system("CLS");
                         
                cout << "**Please enter a valid grade or -99 to finish!\n" << endl;
                cin.clear();
                                     
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            }
        } 
        size++;
    } while (flag != -99);
}

void calcAverage(float grade[], int size)
{
     float least;
     float sum;
     float avg;
     
     least = findLowest(grade, size);
}

float findLowest(float grade[], int size)
{
    int counter = 0;
    float least = 100;
     
    for(counter = 0; counter < size; counter++)
    {
        if(grade[counter] < least)
        {
            least = grade[counter];
        }
    }                                              
         
    return least;
}

Last edited on Mar 31, 2013 at 6:42pm
Topic archived. No new replies allowed.