1

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.

Eugene Wolf
  • 69
  • 1
  • 5
  • Please show a [mre] with your compiler commands and the exact error message – Alan Birtles Jul 28 '19 at 10:39
  • My guess is you were not compiling all of your files into the executable. – drescherjm Jul 28 '19 at 12:32
  • 1
    I don't think `mid = first / last;` does what you think it does. – Eljay Jul 28 '19 at 12:54
  • 1
    And that while loop condition will fail immediate, because `so_far` test is inverted. Regardless, the problem appears to be in your compile/link line, which you did not provide. – Eljay Jul 28 '19 at 13:00
  • 1
    my psychic powers suggest you are trying to compile `binarySearch.cpp` as a program without first compiling main. Instead of `g++ binarySearch.cpp`, compile as `g++ -c binarySearch.cpp`. Then `g++ -c main.cpp` followed by `g++ -o myprogram main.o binarySearch.o` – selbie Jul 28 '19 at 20:15
  • @selbie Thank you so so much!! That worked perfectly!!! I did not realize you had to compile the binarySearch.cpp first, then main.cpp with -c.Sorry, I am still learning and trying to teach myself. > – Eugene Wolf Jul 30 '19 at 23:43

0 Answers0