简体   繁体   English

使用模板构造函数创建模板类对象

[英]Creating a template class object using a template constructor

I'm having trouble creating a class object from a template class in which I need the constructor to also be a template and accept a parameter when the object is created. 我在从模板类创建类对象时遇到问题,在模板类中我需要构造函数也是模板并在创建对象时接受参数。 However, when I attempt to create the object, I receive an error message stating that I'm referencing something that doesn't exist. 但是,当我尝试创建对象时,我收到一条错误消息,指出我引用了一些不存在的内容。

Here's my code: 这是我的代码:

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

and my .cpp: 和我的.cpp:

template <class Node_Type>
BinaryTree<Node_Type>::BinaryTree(Node_Type rootNode) {

    root = rootNode;
    root->left = NULL;
    root->right = NULL;

}

There's more to the .cpp, but it's just other function members that are irrelevant. .cpp还有更多,但它只是其他功能成员无关紧要。 My constructor shown above is what I can't get to work. 我上面显示的构造函数是我无法工作的。

In my main, I'm attempting to declare my object with the call: 在我的主要内容中,我试图通过调用声明我的对象:

BinaryTree<char> node('a');

but when I try this, I get an error message stating: 但是当我尝试这个时,我收到一条错误消息,指出:

undefined reference to `BinaryTree<char>::BinaryTree(char)'

I've been trying to figure this out for two days now. 我一直试图解决这个问题两天了。 I've Googled every topic I can think of and read countless examples on Stack Overflow and other sources with no help. 我用Google搜索了我能想到的每个主题,并在Stack Overflow和其他来源上阅读了无数的例子,没有任何帮助。 Can anyone please explain what my problem is? 谁能解释一下我的问题是什么? I know how to do my project, and I'd be finished by now if the syntax wasn't so ridiculous in C++. 我知道如何完成我的项目,如果语法在C ++中不那么荒谬,我现在就完成了。 Thanks in advance! 提前致谢!

模板代码应该在实例化时可见,这意味着函数的定义也必须在标题中。

Solution: You can't separate template implementations from the header file. 解决方案:您无法将模板实现与头文件分开。 Simply don't use a cpp file and put the definition in your header file (.h file). 只需不要使用cpp文件并将定义放在头文件(.h文件)中。

Why? 为什么? This is because cpp files can become precompiled sources, while templates are compile-time objects; 这是因为cpp文件可以成为预编译源,而模板是编译时对象; therefore, the compiler cannot decide what type to use unless specified. 因此,除非指定,否则编译器无法确定要使用的类型。 So just put all your template undefined implementations in your header .h file. 所以只需将所有模板未定义的实现放在标题.h文件中。

You can force the instantiation of the template in another cpp file. 您可以在另一个cpp文件中强制实例化模板。

BinaryTree<char>;
BinaryTree<int>;
BinaryTree<double>;

That way all the functions do not need to be in header files. 这样,所有功能都不需要在头文件中。 Some people use the extension .inl for the files with the template implementations. 有些人将扩展名.inl用于带有模板实现的文件。 So the .inl file is only needed when the instantiation doesn't already exist. 因此只有在实例化尚不存在时才需要.inl文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM