c++ - 错误 C2664 :cannot convert argument from 'Node<int>* ' to 'int'

标签 c++ templates type-conversion

节点.h:

#include <memory>
using namespace std;

template <typename T>
class Node
{
  private:
     T m_Data;
     shared_ptr<Node<T>> pre_node,next_node;

  public:
     Node(T iData, Node* pre_ptr = nullptr, Node* next_ptr = nullptr) 
       :m_Data(iData),pre_node(make_shared<Node>(pre_ptr)),
        next_node(make_shared<Node>(next_ptr))
};

主要.cpp

#include "Node.h"

int main()
{
   Node<int> head(1);

   system("pause");

   return 0;
}

尝试运行代码时出现错误:

 error C2664: 'Node<int>::Node(const Node<int> &) throw()' : cannot convert argument 1   
 from 'Node<int> *' to 'int'

谁能解释一下这个问题以及纠正它的方法?

最佳答案

问题很可能是对 std::make_shared 的调用:

make_shared<Node>(next_ptr)

在这里,参数应该是一个 Node 或者可以用来构造一个节点的东西(例如,一个 T 或者在你的情况下,一个 int。)你正在传递一个 Node*

不要传递 Node*。传递一个 int 或一个 Node。或者将您的构造函数更改为如下所示:

 Node(T iData, shared_ptr<Node> pre_ptr = nullptr, shared_pre<Node> next_ptr = nullptr) 
   : m_Data(iData),
     pre_node(pre_ptr),
     next_node(next_ptr)

关于c++ - 错误 C2664 :cannot convert argument from 'Node<int>* ' to 'int' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22390854/

相关文章:

php - 将数字字符串转换为浮点型数据

python - 在 python 中使用 csv.DictReader 进行数据类型转换的最快方法

c++ - 是否有一个 c++ 库提供执行外部程序和读取其输出的功能?

c# - 如果我将所有函数都作为类的静态方法是个好主意吗?

php - 为什么我的 Blade 模板内容显示两次?

javascript - 无法调用未定义 Handlebars 问题的方法 'match'

c++ 公历和儒略历继承

c++ - 查找图像中圆的各个中心点

c++ - 在 C++ 中使隐式转换运算符优于另一个

mysql - mysql函数是否可以在输入时接受空数据类型?