C++ 使用模板构造函数创建模板类对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18880700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a template class object using a template constructor
提问by Stephen Fish
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. My constructor shown above is what I can't get to work.
.cpp 有更多内容,但只是其他不相关的函数成员。我上面显示的构造函数是我无法工作的。
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. 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++. Thanks in advance!
这两天我一直在想办法解决这个问题。我在谷歌上搜索了我能想到的每个主题,并在没有帮助的情况下阅读了 Stack Overflow 和其他来源上的无数示例。谁能解释一下我的问题是什么?我知道如何做我的项目,如果 C++ 中的语法不那么荒谬,我现在就完成了。提前致谢!
回答by stefaanv
Template code should be visible at the time of instantiation, meaning that the definition of the functions must also be in the header.
模板代码在实例化时应该是可见的,这意味着函数的定义也必须在标题中。
回答by The Quantum Physicist
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; therefore, the compiler cannot decide what type to use unless specified. So just put all your template undefined implementations in your header .h file.
为什么?这是因为 cpp 文件可以成为预编译源,而模板是编译时对象;因此,除非指定,否则编译器无法决定使用哪种类型。因此,只需将所有未定义的模板实现放在标头 .h 文件中。
回答by QuentinUK
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. So the .inl file is only needed when the instantiation doesn't already exist.
这样所有的函数都不需要在头文件中。有些人将扩展名 .inl 用于带有模板实现的文件。因此,仅当实例化尚不存在时才需要 .inl 文件。