2015-12-18 9 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>   ] 

Node Class

#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::string. В классе std::string нет конструктора, который принимает один символ в качестве аргумента, поэтому не может происходить неявное преобразование.

Поэтому компилятор бросает

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

на вас.