【问题标题】:Explicit Instantiation Not Working Properly显式实例化无法正常工作
【发布时间】:2018-09-09 00:32:39
【问题描述】:

我遇到了一个奇怪的错误。我似乎正确地使用了显式实例化,但是在编译时出现“未解析的外部符号”错误。

这是正在发生的事情:

code.h

#pragma once

template <typename T>
struct A {
    void foo() const;
};

template <typename T>
struct B : public A<T> {};

typedef B<int> C;

code.cpp

#include "code.h"

template <typename T>
void A<T>::foo() const {}

template struct B<int>;

ma​​in.cpp

#include "code.h"

int main() {

    C test;
    test.foo()  // <----- unresolved external symbol

    return 0;

}

据我所知,我所做的几乎与this guy 所做的完全一样,只是将继承加入其中。为什么会出错?如果有帮助,我正在使用 Visual Studio 2017。

【问题讨论】:

    标签: c++ templates visual-c++


    【解决方案1】:

    我想通了。如果您在 code.h 中查看此处:

    template <typename T>
    struct B : public A<T> {};
    

    你看我用的是A&lt;T&gt;,也需要显式实例化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 2016-01-01
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多