我的类节点是一个简单树的基本实现,其中每个节点都有两个子节点。我已经阅读了 10 个关于错误消息的答案,但我无法弄清楚哪里出了问题。
error: no matching function for call to '
Lib::Node<Math::Expression*>::Node(Math::Expression*&)
'
Lib::Node< Math::Expression* > Dirac::DiracParser::parse(std::string str) const{
typedef Math::Expression* Expr; // Don't do this, as pointed out by Lightness Races in Orbit
typedef Lib::Node<Expr> ExprNode;
ExprNode root; // works
Expr e = new Math::UnknownExpression();
ExprNode root2(e); // yields error message
return root;
}
定义
namespace Lib{
template <typename T>
class Node{
public:
Node() : empty(true){}
Node(T* t) : data(t), empty(false){}
Node(T* t, Node<T>* l) : data(t), left(l), empty(false){}
Node(T* t, Node<T>* l, Node<T>* r) : data(t), left(l), right(r), empty(false){}
Node(const Node<T>& n) : data(n.data), left(n.left), right(n.right), empty(false){}
inline bool isEmpty(){
return empty;
}
~Node(){}
T* data;
Node* left;
Node* right;
private:
bool empty;
};
}
据我了解,该错误是指我的第二个构造函数未被实现。我想我可能需要做一些指针体操才能正确通过它,但我不知道怎么做。
最佳答案
你实例化了Lib::Node< Math::Expression* >
, 所以 T
是Math::Expression*
.构造函数接受 T*
.
当您替换 T
时, 参数 T*
变成 Math::Expression**
.您尝试传递 Math::Expression*
给构造函数,但没有构造函数接受它。
关于c++ - 没有要用构造函数调用的匹配函数(T* 数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49121530/