0

谁能告诉我是什么导致了这个错误?

/tmp/ccHWwGhh.o: In function `main': A2.cpp:(.text+0x407): undefined reference to `binarysearch(std::string, std::vector<std::string, std::allocator<std::string> >)' collect2: error: ld returned 1 exit status

这是我的代码:

//Located before main()
void binarysearch(string key, vector<string>& f2);

//Located in main()
binarysearch(key, file2);
//key is a string, file2 is a vector<string>

//Here is my code defining the function:
void binaraysearch(string key, vector<string> f2){
    sort_vector(f2);
    int mid = 0;
    int left = 0;
    int right = f2.size();
    bool found = false;
    while (left < right){
            mid = left + (left+right)/2;
            if (key > f2[mid]){
                    left = mid + 1;
            }
            else if(key < f2[mid]){
                    right = mid;
            }
            else{
                    found == true;
            }
    }
    if (found == true){
            cout << "YES: " << key << endl;
    }
    else{
            cout << " NO: " << key << endl;
    }
}
4

2 回答 2

2

一个错字(或两个):

//Located before main()
void binarysearch(string key, vector<string>& f2);

//Located in main()
binarysearch(key, file2);
//key is a string, file2 is a vector<string>

//Here is my code defining the function:
void binaraysearch(string key, vector<string> f2){
          ^
          here

函数定义中的 a 太多。vector<string>a和which之间也存在差异,vector<string>&这也无济于事。

于 2013-09-15T04:05:53.153 回答
2

函数的声明如下,

void binarysearch(string key, vector<string>& f2);

在函数定义中它已经变成了,

void binaraysearch(string key, vector<string> f2){

}

这可能是类型不匹配。

于 2013-09-15T04:06:23.437 回答