我在从模板类创建类对象时遇到问题,在模板类中我需要构造函数也是模板并在创建对象时接受参数.但是,当我尝试创建对象时,我收到一条错误消息,指出我引用了一些不存在的内容.
这是我的代码:
using namespace std;
#include <cstdlib>
template <class Node_Type>
class BinaryTree
{
public:
BinaryTree(Node_Type);
BinaryTree(Node_Type, Node_Type);
BinaryTree(Node_Type, Node_Type, Node_Type);
bool isEmpty();
Node_Type info();
Node_Type inOrder();
Node_Type preOrder();
Node_Type postOrder();
private:
struct Tree_Node
{
Node_Type Node_Info;
BinaryTree<Node_Type> *left;
BinaryTree<Node_Type> *right;
};
Tree_Node *root;
};
#endif
Run Code Online (Sandbox Code Playgroud)
和我的.cpp:
template <class Node_Type>
BinaryTree<Node_Type>::BinaryTree(Node_Type rootNode) {
root = rootNode;
root->left = NULL;
root->right = NULL;
}
Run Code Online (Sandbox Code Playgroud)
.cpp还有更多,但它只是其他功能成员无关紧要.我上面显示的构造函数是我无法工作的.
在我的主要内容中,我试图通过调用声明我的对象:
BinaryTree<char> node('a');
Run Code Online (Sandbox Code Playgroud)
但是当我尝试这个时,我收到一条错误消息,指出:
undefined reference to `BinaryTree<char>::BinaryTree(char)'
Run Code Online (Sandbox Code Playgroud)
我一直试图解决这个问题两天了.我用Google搜索了我能想到的每个主题,并在Stack Overflow和其他来源上阅读了无数的例子,没有任何帮助.谁能解释一下我的问题是什么?我知道如何完成我的项目,如果语法在C++中不那么荒谬,我现在就完成了.提前致谢!