2015-12-18 40 views
1

我想增加节点的数量,但出现以下错误。如何解决该问题?链接列表增加节点的数量

error C2664: 'node<T>::node(const T &,const T &,const T &,node<T> *)' : cannot convert parameter 1 from 'char' to 'const std::string &' 
     with 
     [ 
1>    T=std::string 
1>   ] 
1>   Reason: cannot convert from 'char' to 'const std::string' 
1>   No constructor could take the source type, or constructor overload resolution was ambiguous 
1>   yeni.cpp(21) : see reference to function template instantiation 'void f<std::string>(T)' being compiled 
1>   with 
1>   [ 
1>    T=std::string 
1>   ] 

节点类

#ifndef NODE_CLASS 
#define NODE_CLASS 

#ifndef NULL 
#include <cstddef> 
#endif // NULL 

// linked list node 
template <typename T> 
class node 
{ 
    public: 
     T nodeValue,nodeValue2,nodeValue3;  // data held by the node 
     node<T> *next; // next node in the list 

     // default constructor with no initial value 
     node() : next(NULL) 
     {} 

     // constructor. initialize nodeValue and next 
     node(const T& item, const T& item2, const T& item3, node<T> *nextNode = NULL) : 
       nodeValue(item),nodeValue2(item2),nodeValue3(item3), next(nextNode) 
     {} 
}; 


#endif // NODE_CLASS 

    #include <iostream> 
    #include <string> 
    #include "d_node.h" 

    using namespace std; 

    template<typename T> 
    void f(T s){ 
     node <T>*front=NULL; 
     front=new node<T>(s[0],s[1],s[2]); 

    } 

Main.cpp的

 int main() { 

      string string; 

      cout << "Enter the string:"; 
      cin >>string; 

      f(string); 

      return 0; 
     } 
+0

您创建了一个'新节点',但是您不会将'T'参数传递给构造函数。你想传递'string'还是'char'?你必须决定! –

回答

0

要传递char类型的参数,其预计const T&类型参数的构造函数,在这种情况下,与T = std::stringstd::string类中没有构造函数,它只接受一个字符作为参数,因此不会发生隐式转换。

因此,编译器在你抛出

Reason: cannot convert from 'char' to 'const std::string' 
1> No constructor could take the source type,