<i id='QBo9P'><tr id='QBo9P'><dt id='QBo9P'><q id='QBo9P'><span id='QBo9P'><b id='QBo9P'><form id='QBo9P'><ins id='QBo9P'></ins><ul id='QBo9P'></ul><sub id='QBo9P'></sub></form><legend id='QBo9P'></legend><bdo id='QBo9P'><pre id='QBo9P'><center id='QBo9P'></center></pre></bdo></b><th id='QBo9P'></th></span></q></dt></tr></i><div id='QBo9P'><tfoot id='QBo9P'></tfoot><dl id='QBo9P'><fieldset id='QBo9P'></fieldset></dl></div>
    • <bdo id='QBo9P'></bdo><ul id='QBo9P'></ul>
    <tfoot id='QBo9P'></tfoot>

    <small id='QBo9P'></small><noframes id='QBo9P'>

        <legend id='QBo9P'><style id='QBo9P'><dir id='QBo9P'><q id='QBo9P'></q></dir></style></legend>
      1. 为什么 C++ 模板定义需要在头文件中?

        时间:2023-05-24
        <legend id='fkMYF'><style id='fkMYF'><dir id='fkMYF'><q id='fkMYF'></q></dir></style></legend>

            <bdo id='fkMYF'></bdo><ul id='fkMYF'></ul>

            <small id='fkMYF'></small><noframes id='fkMYF'>

              <i id='fkMYF'><tr id='fkMYF'><dt id='fkMYF'><q id='fkMYF'><span id='fkMYF'><b id='fkMYF'><form id='fkMYF'><ins id='fkMYF'></ins><ul id='fkMYF'></ul><sub id='fkMYF'></sub></form><legend id='fkMYF'></legend><bdo id='fkMYF'><pre id='fkMYF'><center id='fkMYF'></center></pre></bdo></b><th id='fkMYF'></th></span></q></dt></tr></i><div id='fkMYF'><tfoot id='fkMYF'></tfoot><dl id='fkMYF'><fieldset id='fkMYF'></fieldset></dl></div>

                  <tbody id='fkMYF'></tbody>

                <tfoot id='fkMYF'></tfoot>

                1. 本文介绍了为什么 C++ 模板定义需要在头文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  可能的重复:
                  为什么应该模板类的实现和声明在同一个头文件中?

                  例如在定义模板类时,为什么类方法的实现需要在标题中?为什么它们不能在实现文件 (cpp/cxx) 中?

                  e.g when defining a template class why do the implementations of the class methods need to be in the header? Why can't they be in a implementation file (cpp/cxx)?

                  推荐答案

                  模板类不是类,而是可用于创建类的模板.当你实例化这样一个类时,例如MyTemplate,编译器当场创建类.为了创建它,它必须查看所有模板化的成员函数(以便它可以使用模板创建实际的成员函数,例如 MyTemplate::foo() ),因此这些模板化的成员函数必须在头文件中.

                  A template class is not a class, it's a template that can be used to create a class. When you instantiate such a class, e.g. MyTemplate<int>, the compiler creates the class on the spot. In order to create it, it has to see all the templated member functions (so that it can use the templates to create actual member functions such as MyTemplate<int>::foo() ), and therefore these templated member functions must be in the header.

                  如果成员不在头文件中,编译器将简单地假设它们存在于其他地方,并从模板化的函数声明中创建实际的函数声明,这会导致链接器错误.

                  If the members are not in the header, the compiler will simply assume that they exist somewhere else and just create actual function declarations from the templated function declarations, and this gives you linker errors.

                  "export" 关键字应该可以解决这个问题,但很少有编译器支持它(我只知道 Comeau).

                  The "export" keyword is supposed to fix this, but few compilers support it (I only know of Comeau).

                  您也可以显式实例化 MyTemplate - 然后编译器会在编译包含 MyTemplate 成员函数定义模板.

                  You can also explicitly instantiate MyTemplate<int> - then the compiler will create actual member functions for MyTemplate<int> when it compiles the cpp files containing the MyTemplate member function definition templates.

                  这篇关于为什么 C++ 模板定义需要在头文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在检测习语中要求精确的函数签名? 下一篇:后备可变参数构造函数 - 为什么这有效?

                  相关文章

                  最新文章

                  <i id='TsoZt'><tr id='TsoZt'><dt id='TsoZt'><q id='TsoZt'><span id='TsoZt'><b id='TsoZt'><form id='TsoZt'><ins id='TsoZt'></ins><ul id='TsoZt'></ul><sub id='TsoZt'></sub></form><legend id='TsoZt'></legend><bdo id='TsoZt'><pre id='TsoZt'><center id='TsoZt'></center></pre></bdo></b><th id='TsoZt'></th></span></q></dt></tr></i><div id='TsoZt'><tfoot id='TsoZt'></tfoot><dl id='TsoZt'><fieldset id='TsoZt'></fieldset></dl></div>
                2. <tfoot id='TsoZt'></tfoot>

                    • <bdo id='TsoZt'></bdo><ul id='TsoZt'></ul>

                      <legend id='TsoZt'><style id='TsoZt'><dir id='TsoZt'><q id='TsoZt'></q></dir></style></legend>

                      <small id='TsoZt'></small><noframes id='TsoZt'>