c++ - "Implicit instantiation of undefined template"前向声明模板类时

标签 c++ templates clang forward-declaration

我有一些代码需要在其中前向声明一个模板类(或者至少,前向声明对我来说会让事情变得更容易......)。我已经编写了我遇到的问题的简化版本,所以我可以在这里显示它:

template<bool>
class MyTemplateClass;

int main( int argc, char* argv[] )
{
    MyTemplateClass<false> myTemp;  // error here
    myTemp.GetTheValue();
    return 0;
}

template<bool bShouldMult>
class MyTemplateClass
{
    int m_myint;
    float m_myfloat;

public:
    MyTemplateClass() : m_myint(5), m_myfloat(3.0f) {}
    float GetTheValue()
    {
        return m_myint * (bShouldMult ? m_myfloat : 1.0f);
    }   

};

我在注释行遇到的错误是:

Error - implicit instantiation of undefined template 'MyTemplateClass<false>'

我还需要在 MyTemplateClass 的前向声明中包含哪些其他细节?由于错误不是来自下一行,我假设这不是因为方法未定义。我使用的编译器是 LLVM/CLang,我在 Mac 上编译。

最佳答案

你忘了#include什么吗?

我忘记了之后得到了这个

#include <array>

当使用 std::array

:^)

关于c++ - "Implicit instantiation of undefined template"前向声明模板类时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12797051/

相关文章:

c++ - error_code 的格式说明符是什么?

c++ - QListWidget 带有可在项目单击时检查的项目

c++ - LibClang clang_getArgType() 返回错误的类型

c - 是否可以从代码中指定链接器选项?

c++ - 包括带有 Clang/LLVM 的 C++ 11 头文件

c++ - SIFT 匹配给出非常差的结果

c++ - 为什么我不能用非引用返回类型定义 operator=?

c++ - 具有 32 位/64 位整数重载的模板函数

node.js - 如何迭代 SendGrid 模板中的对象数组?

c++ - 收到基于模板类及其子类函数调用的错误