简体   繁体   中英

C++ Vectors: Function not viable + No matching function

So, it seems every time I try to compile, I am running into an issue involving

"error: no matching function for call to 'search_and_report'"

   while(name!="!")
   {
      label = "linear_search";
      search_and_report(names,n,name,label,linear_search);

      label = "binary_search";
      search_and_report(names,n,name,label,binary_search);

      cout << "Search for name: (! when done): ";
      getline(cin,name);
   }

And I suspect it is my initialization of the vector that is causing the problem.

{
   int n = 0;
   string label;
   string name;
   vector<string> names(n);

   // start program
   cout << "Enter a name (! when done): ";
   getline(cin,name);

   while(name!="!")
   {
      n++;
      names.push_back(name);
      cout << "Enter a name (! when done): ";
      getline(cin,name);
   }

I have been reading from this site: http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm and it says that I should be allowed to place my "n" in push_back to allow it to grow. But if I'm honest, I'm not sure I'm understanding vectors as well as I should.

EDIT As mentioned in the comments, I have removed the external code, and as asked, I will provide the function signature.

void search_and_report(std::vector<const string> names[], int n, string name, string label,
                       bool (*search)(vector<const string> names[], int n, string name,
                                      int &count))
void search_and_report(vector<const string> names[], ...)
...
vector<string> names(n);
...
search_and_report(names,...);

You've declared search_and_report to take a pointer to an array of vector<const string> s, but you're passing it a vector<string> . The compiler can't find an appropriate function to call because your types don't match.

From the link you've provided, the signature of the function search_and_report is:

void search_and_report(vector<const string> names[], int n, string name,
                       string label,
                       bool (*search)(vector<const string> names[], int n,
                                    string name, int &count));

The first argument is not a vector, is array of vectors.

Based on what I've read, you should change the function to take a const std::vector<std::string>& (see signatures of binary_search and linear_search ).

Note: Using std::vector<const string> makes no sense because the vector handles all the memory allocation for the string objects it holds. I think you want to use const std::vector<std::string>& as I wrote above.

Update: About binary search.

If the vector is ordered, you can perform a binary search this way (pseudocode):

bool binary_search(vector, str, begin, end) {
    int m = (end + begin) / 2;

    if (vector[m] < str) {
        return binary_search(begin, m-1);
    }

    if (vector[m] > str) {
        return binary_search(m+1, end);
    }

    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM