为什么会出现此错误:未在此范围内声明 我有这个C++代码,包含以下错误: 1. readBinaryFile未在此范围内声明 2. ret未在此范围内声明 3. 递归二进制搜索未在此范围内声明

为什么会出现此错误:未在此范围内声明 我有这个C++代码,包含以下错误: 1. readBinaryFile未在此范围内声明 2. ret未在此范围内声明 3. 递归二进制搜索未在此范围内声明,c++,binary-search,C++,Binary Search,有人能帮我理解为什么会出现这些错误吗 先谢谢你 BinarySearch.h #ifndef BINARYSEARCH_H #define BINARYSEARCH_H using namespace std; class BinarySearch { public: char readBinaryFile(char *fileName); int recursiveBinarySearch(

有人能帮我理解为什么会出现这些错误吗

先谢谢你

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) ;
       }
    }
#包括
#包括
#包括
#包括
使用名称空间std;
//要动态存储大量值的向量。。。
向量ret;
//用于计算比较次数的变量。。。
int n=0;
//方法读取文件。。。。
void readBinaryFile(char*文件名)
{
ifstream读取(文件名);
无符号整数电流;
如果(读取!=NULL)
while(读>>当前){
反向推回(当前);
}
}
//递归二进制搜索法
无效递归二进制搜索(int开始索引、int结束索引、int目标值)
{
int mid=(开始索引+结束索引)/2;
如果(开始索引>结束索引)
{

cout在.cpp文件中,您错过了为
BinarySearch::

void BinarySearch::readBinaryFile(char* fileName)
{
   //code here
}
如上所述,由于这些方法不是静态的,所以需要一个对象来调用它们

BinarySearch x;
x.readBinary(...);

binarySearch.cpp不包含.h文件欢迎使用堆栈溢出!听起来你可能会使用@AndyG,我无意中在代码中没有包含它,但原始代码中包含了它。@h.choi:以后定义函数时别忘了定义函数的范围。否则编译器怎么知道
recursiveBinarySearch
应该是b对于
BinarySearch
对象,您还需要实际创建类的实例,然后才能使用其函数。
BinarySearch::readBinaryFile
也许您更习惯于Java或其同类,但这些函数可以自由浮动;它们不需要是类的一部分。也许我应该阅读所有注释--t他已经给出了答案。为什么大多数人用评论而不是“真实”的答案来回答,这样问题也可以标记为已回答!?
BinarySearch x;
x.readBinary(...);