2017-01-02 44 views
0
list<SimpleList<int> *> listSLi; 
list<SimpleList<double> *> listSLd; 
list<SimpleList<string> *> listSLs; 

这是主要的:错误:没有匹配函数调用find_name

`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中类。派生类是堆栈和队列。

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) - > 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(); 
          ^
+0

请在您的问题的main.cpp中标记312行 –

+0

您是否确实需要'find_name'作为模板函数?无论如何,你现在只返回'SimpleList *'。 – ForceBru

+0

我没有显示整个功能,因为它会太长,但我在代码的其余部分使用双打和字符串。 – user1692570

回答

3

作为错误信息的SAI,编译器不能推断出类型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你想牛逼这里,或直接供应类型像find_name<string>(...)

我没整明白的目的,但不会忽略错误:)

增强:

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

相关问题