How Can I Fix Undefined Reference Error In Searching Program?

I have a homework assignment in which I do a linear search and a binary search.
I am getting an error from the comparisons variable that calls the linearSearchBench and binarySearch bench functions. I'm also getting an Id returned 1 exit status. Can anyone help me to find out how to troubleshoot the errors? And suggestions on how to fix 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  // Search Benchmarks
#include <iostream>
using namespace std;

// Global Constants
const int ARRAY_SIZE = 20;	// The array size

// Function prototype
int linearSearchBench(int [], int, int);
int binarySearchBench(int [], int, int);
int searchFor = 521; // declared and assigned value to be searched for

int main()
{
	int comparisons;	// To hold the number of comparisons.

	// Initialize an array with 20 integer values.
	int tests[ARRAY_SIZE] = { 101, 142, 147, 189, 199, 207, 222,
							  234, 289, 296, 310, 319, 388, 394,
							  417, 429, 447, 521, 536, 600 };


	// Display the value being searched for.
	cout << "Searching for the value 521 ";

	// Perform the linear search.
	comparisons = linearSearchBench(tests, ARRAY_SIZE, searchFor);

	// Display the results of the linear search.
	cout << "The linear search made " << comparisons;
	cout << " comparisons.\n";

	// Perform the binary search.
	comparisons = binarySearchBench(tests, ARRAY_SIZE, searchFor);

	// Display the results of the binary search.
	cout << "The binary search made " << comparisons;
	cout << " comparisons.\n";

	return 0;
}

// user defined functions
int linearSearchBench(const int testsLin[], int numElem, int searchValue)  { // created the linearSearchBench function to do a linear search of the array
    int i; // initialized the counter variable
    for (i = 0; i < numElem; i++) // for loop to loop through array
        if (testsLin[i] == searchValue)
        return i;
    return -1;
}

int binarySearchBench(const int testsBin[], int numElems, int searchValue) { // created the binSearch function to do a binary search of the array
    int first = 0;
    int last = numElems -1;
    int middle = 0;
    int position = -1;
    bool found = false;

    while (!found && first <= last) {
        middle = (first + last) / 2;
        if (testsBin[middle] == searchValue) {
        found = true;
        position = middle;
        }
        else if (testsBin[middle] > searchValue)
        last = middle -1;
        else first = middle + 1;
    }
    return position;
}

Those error messages are telling you the linker can't find the "referenced" function.

Do you know that when dealing with function parameters a const int* is not the same as an int*?

Do you also know that a function prototype and the function implementation must agree as to the number and type of parameters?


I didn't know or forgot that the function prototype and the function definition that the type of parameters had to be the same. With just that little change, the program compiled and executed perfectly.

Thank you for helping me with this!
I didn't know or forgot that the function prototype and the function definition that the type of parameters had to be the same.

Just a little "nit", a function prototype is a function definition, a function declaration is the function implementation.

closed account (1vRz3TCk)
jlb wrote:
Just a little "nit", a function prototype is a function definition, a function declaration is the function implementation.

Haven't you got them back'uds?
1
2
3
4
5
6
int func(int *); // function declaration or Function Prototype

int func(int *a)  // function definition or function implementation
{
    return *a;
}
Last edited on
Yea, my dyslexia, is kicking in again. That's why I usually use prototype and implementation instead of the "real" terms.

Topic archived. No new replies allowed.