-2

I have this C++ code that contains the following errors:
1. readBinaryFile was not declared in this scope
2. ret was not declared in this scope
3. recursiveBinarySearch was not declared in this scope.

Can someone please help me understand why these errors are coming up?

Thank you in advance.

BinarySearch.h

    #ifndef BINARYSEARCH_H
    #define BINARYSEARCH_H

    using namespace std;

    class BinarySearch
    {

       public:
           char readBinaryFile(char *fileName);
           int recursiveBinarySearch(int start_index, int end_index, int        targetValue);
    };

    #endif

BinarySearch.cpp

    #include<iostream>
    #include<fstream>
    #include<cstdlib>
    #include<vector>
    using namespace std;

    //vector to dynamically store large number of values...
    vector<unsigned int> ret; 

    //variable to count the number of comparisions...  
    int n=0;  
    //method to read file....  
    void readBinaryFile(char* fileName)
    {
        ifstream read(fileName);
        unsigned int current;
        if(read!=NULL)
        while (read>>current) {
            ret.push_back(current);
        }

    }

    //recursive binary search method

    void recursiveBinarySearch(int start_index, int end_index, int targetValue)
    {

       int mid = (start_index+end_index)/2;

       if(start_index>end_index)
       {
          cout<<n<<":-"<<endl;
           return ;  
       }

       if(ret.at(mid)==targetValue)
       {
          n++;
           cout<<n<<":"<<mid<<endl;
           return ;  
       }
       else if(ret.at(mid)<targetValue)
       {
           n=n+2;//upto here two comparisions have been made..so adding two
           start_index=mid+1;
           return recursiveBinarySearch(start_index,end_index,targetValue) ;
       }
       else
       {
           n=n+2;
           end_index=mid-1;
           return recursiveBinarySearch(start_index,end_index,targetValue) ;
       }
    }

BinarySearch_test.cpp

    #include "BinarySearch.h"
    #include <iostream>
    #include <fstream>
    using namespace std;

    int main()
    {

       char a[] ="dataarray.bin";
       //assuming that all integers in file are already in sorted order
       readBinaryFile(a);

       int targetValue;
       cout<<"Enter a Value to search:";
       cin>>targetValue;

       recursiveBinarySearch(0,ret.size()-1,targetValue) ;


       return 0;

    }
Rama
  • 3,222
  • 2
  • 11
  • 26
H.choi
  • 7
  • 1
  • 1
    binarySearch.cpp doesn't include the .h file – AndyG May 15 '17 at 16:34
  • 4
    Welcome to Stack Overflow! Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver May 15 '17 at 16:35
  • @AndyG I did not include that on the code here by accident but the original code has it. – H.choi May 15 '17 at 16:37
  • 1
    @H.choi: Don't forget to scope your functions when you define them later. How else will the compiler know that `recursiveBinarySearch` is supposed to be for a `BinarySearch` object? Also, you need to actually create an instance of the class before you can use its functions. `BinarySearch::readBinaryFile` Perhaps you are more used to Java or its ilk, but those functions can be free-floating; they don't need to be part of a class. – AndyG May 15 '17 at 16:44

1 Answers1

0

In your .cpp file you miss the class name to define your functions for BinarySearch::

void BinarySearch::readBinaryFile(char* fileName)
{
   //code here
}

and as mentioned above, since the methods are not static you will need an object to call them.

BinarySearch x;
x.readBinary(...);
florgeng
  • 856
  • 1
  • 9
  • 17
  • maybe I should have read all the comments -- the answer was already given. Is there a reason why most people answer by comments instead of "real" answers, so that the question can also be marked as answered!? – florgeng May 15 '17 at 19:51