错误:没有匹配函数来调用find_name

时间:2017-01-02 18:01:32

标签: c++

list<SimpleList<int> *> listSLi;
list<SimpleList<double> *> listSLd;
list<SimpleList<string> *> listSLs;

这是主要的:

`int main() {
      cout << "Enter name of input file: ";
      string input_f;
      cin >> input_f;

  /*  cout << "Enter name of output file: ";
  string output_f;
  cin >> output_f;
  */
  ifstream input(input_f);
  //  ofstream output(output_f); // Make this a separate function

  string to_parse;
  vector<string> sep_words;
  while (getline(input, to_parse)) {
    //    cout << "PROCESSING COMMAND: " << to_parse << '\n';
    to_parse += '\n'; // getline removes the \n on each line
    sep_words = parse_line(to_parse);
    cpp(sep_words);
  }
  return 0;
}

这是SimpleList类。派生类是Stack和Queue。

template<typename T>
class SimpleList {
public:
  SimpleList();
  SimpleList(const string& n);
  virtual void push(T value) =0;
  virtual void pop() =0;
  void set_name(const string&);
  string get_name();
protected:
  void insert_front(T);
  void insert_back(T);
  void remove_front();
  // Should be protected:
  Node<T> first, last, temp;
  string name;
  int size;
};

sep_words将包含2或3个字符串。

void cpp(const vector<string>& single_words) {/////////////////////////
   if (single_words.size() == 2) { // pop
     switch(single_words[1][0]) {
     case 'i':
       if(is_name_in(single_words[1], 0) != true) {
         cout << "ERROR: This name does not exist!\n";
         return;
       }
       else if (is_list_empty(single_words[1], 0)) { // add == true for readability
         cout << "ERROR: This list is empty!\n";
           return;
       }
       else {
 312        find_name(single_words[1], 0)->pop();
       }
       break;

find_name(single_words [1],0) - &gt; pop();问题是

template<typename T>
SimpleList<T>* find_name(const string& nm, int which_type) { // Can do char which_type instead
  // 0 stands for integer 1 stands for double 2 stands for string
  switch(which_type) {
  case 0:
    for (list<SimpleList<int> *>::iterator it = listSLi.begin(); it != listSLi.end(); ++it) {
      if ((*it)->name == nm) { // Use get_name instead
        return *it;
      }
    }
    break;
  case 1:
    for (list<SimpleList<double> *>::iterator it = listSLd.begin(); it != listSLd.end(); ++it) {
      if ((*it)->name == nm) {
        return *it;
      }
    }
    break;
  case 2:
    for (list<SimpleList<string> *>::iterator it = listSLs.begin(); it != listSLs.end(); ++it) {
      if ((*it)->name == nm) {
        return *it;
      }
    }
    break;
  }
}

这是编译器错误:

main.cpp: In function ‘void cpp(const std::vector<std::basic_string<char> >&)’:
main.cpp:312:31: error: no matching function for call to ‘find_name(const value_type&, int)’
   find_name(single_words[1], 0)->pop();
                               ^
main.cpp:312:31: note: candidate is:
main.cpp:272:16: note: template<class T> SimpleList<T>* find_name(const string&, int)
 SimpleList<T>* find_name(const string& nm, int which_type) { // Can do char which_type instead
                ^
main.cpp:272:16: note:   template argument deduction/substitution failed:
main.cpp:312:31: note:   couldn't deduce template parameter ‘T’
   find_name(single_words[1], 0)->pop();
                               ^

1 个答案:

答案 0 :(得分:3)

作为错误消息sais,编译器无法推断出模板中的类型T.

template<typename T>
SimpleList<T>* find_name(const string& nm, int which_type); ....

这里类型T是函数的参数。但编译器无法知道您的意思,因为它不会出现在参数中。

 const string& nm, int which_type // what shall be T?

所以也许代替string你想要T,或直接提供类型find_name<string>(...)

我不明白整个目的,但是这解决了错误:)

增强:

标志which_type也可能是不必要的,因为调度可以在过载级别完成。请参阅标记(例如stl中的iterator_tags)和一般的重载。