Linked List增加节点数

时间:2015-12-18 23:26:32

标签: c++ linked-list nodevalue

我想增加节点数但是我收到以下错误。如何解决问题?

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;
        }

1 个答案:

答案 0 :(得分: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,

在你身边。