[Linker error] undefined reference

I'm having a hard time trying to decipher this problem after several hours of research.

[Linker error] undefined reference to `averageNums(int*, int, double&)'
ld returned 1 exit status

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
// This program demonstrates an array being passed to a function.
 #include <iostream>
 #include <iomanip>
 using namespace std;

 void showValues(int [], int); // Function prototype
 void inputValues(int[], int);
 void highestLowest(int[],int);
 void averageNums(int[],int,double&);
 
 int main()
 {
 double average;   
 const int ARRAY_SIZE = 10;
 int numbers[ARRAY_SIZE];
 
 inputValues(numbers, ARRAY_SIZE);

 showValues(numbers, ARRAY_SIZE);
 
 highestLowest(numbers, ARRAY_SIZE);

 averageNums(numbers, ARRAY_SIZE,average);
 
 system ("pause");
 return 0;
 }
 
 void showValues(int nums[], int size)
 {
      cout<<setw(36)<<"Values In Data Set"<<endl;
 for (int index = 0; index < size; index++)
 cout << nums[index] << setw(6);
 cout << endl<<endl;
 }

 void inputValues(int nums[], int size)
 {
      
      for (int index=0; index < size; index++)
      {
      cout<<"please enter number "<<index+1<<" :";
      cin>>nums[index];
      cout <<endl;
      }
}

void highestLowest(int nums[], int size)
{
     int count;
//highest
int highest;
highest = nums[0];
for (count = 1; count < size; count++)
{
if (nums[count] > highest)
highest = nums[count];
}
//lowest
int lowest;
lowest = nums[0];
for (count = 1; count < size; count++)
{
if (nums[count] < lowest)
lowest = nums[count];
}
cout <<"Values used range from "<<lowest<<" to "<<highest<<endl;
}

void averageNums(int nums[], int size,double avg)
{
     double total = 0; // Initialize accumulator
//double average; // Will hold the average
for (int count = 0; count < size; count++)
total += nums[count];
avg = total / size;

}
Your forward declarations for agerageNums and your implementation don't argree.
Line 9:
 
void averageNums(int[],int,double&);

Line 70:
 
void averageNums(int nums[], int size,double avg)

In the forward. the third argument is double by reference. In the implementation, it is passed by value. As far as the linker is concerned, these are two different functions. A forward declaration and it's implementation must match exactly.
Wow, thank you very much for your help.....a second set of eyes was a great help.


~Eric
Topic archived. No new replies allowed.