节点.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/