c++ - 链表增加节点数

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

最佳答案

您正在将类型为char的参数传递给构造函数,该构造函数需要类型为const T&的参数,在本例中为T = std::stringstd::string类中没有使用单个字符作为参数的构造函数,因此不会发生隐式转换。

因此,编译器抛出

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


在你身上

关于c++ - 链表增加节点数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34365452/

相关文章:

C++方法参数按引用传递-内存问题

C++私有(private)构造函数

java - 按字母顺序将新对象插入到 Linkedlist(of Strings) 中,而不进行排序

c - 结构效率

c++ - 显式默认的 constexpr ctor 是否应该允许非 constexpr 初始化

c++ - 是否可以在 C++ 中为嵌入式平台创建新的原始类型?

linked-list - 在链接列表中查找最大值/最小值

javascript - "nodeValue"文本节点的属性为空 - 那么我该如何测试它?

Php - Dom,在 nodeValue 中插入 HTML_CODE

php - DOMDocument 中的 nodeValue 在 PHP 中返回奇怪的字符