Try to teach myself C++. I'm trying to create a binary search class, but I keep getting "undefined reference to" errors for some reason. When I try to compile main.cpp, I get the following error is below.
/tmp/ccJwc0lu.o: In function `main':
main.cpp:(.text+0x11b): undefined reference to `binarySearch::binSearch(int*, int, int)'
collect2: error: ld returned 1 exit status
I'm honestly not sure what I am doing wrong here. I am very very new to C++, and so I have just been watching youtube videos are doing my best to create my own data structures.
binarySearch.h
class binarySearch
{
private:
int first;
int last;
int position = -1;
bool so_far = false;
public:
int binSearch(int ra[], int size, int value);
};
binarySearch.cpp
#include "binarySearch.h"
int binarySearch::binSearch(int ra[], int size, int value) {
int mid = 0;
first = 0;
last = size-1;
while (first <= last && so_far != false) {
mid = first / last;
if (ra[mid] == value) {
so_far = true;
position = mid;
}
else if (value < ra[mid]) {
last = mid - 1;
}
else if (value > ra[mid]) {
first = mid + 1;
}
}
return position;
}
main.cpp
#include <iostream>
#include "binarySearch.h"
using namespace std;
int main() {
int SIZE = 5;
int ra[SIZE] = {1, 2, 3, 4, 5};
int results;
int value = 5;
binarySearch bsh;
results = bsh.binSearch(ra, SIZE, value);
cout << results << std::endl;
return 0;
}
The result to simply display the position of the index if "value" is found in the int array.