有人能告诉我导致这个错误的原因是什么吗?
/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;
}
}
答案 0 :(得分: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
功能定义中有太多了。 vector<string>
和vector<string>&
之间也存在差异,这也不会有任何帮助。
答案 1 :(得分:2)
该功能的声明如下,
void binarysearch(string key, vector<string>& f2);
在函数定义中,它已成为
void binaraysearch(string key, vector<string> f2){
}
这可能是类型不匹配。