Reputation: 25
Node.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))
};
main.cpp
#include "Node.h"
int main()
{
Node<int> head(1);
system("pause");
return 0;
}
I get an error when try to run the code:
error C2664: 'Node<int>::Node(const Node<int> &) throw()' : cannot convert argument 1
from 'Node<int> *' to 'int'
Can someone explain the problem and the way to correct it?
Upvotes: 0
Views: 2353
Reputation: 227370
The problem is most likely the call to std::make_shared
:
make_shared<Node>(next_ptr)
Here, the argument should be a Node
or something that can be used to construct one (for instance, a T
or specifically in your case, an int
.) You are passing a Node*
.
Don't pass a Node*
. Pass an int
or a Node
. Or change your constructor to something like this:
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)
Upvotes: 2