【发布时间】: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>;
main.cpp
#include "code.h"
int main() {
C test;
test.foo() // <----- unresolved external symbol
return 0;
}
据我所知,我所做的几乎与this guy 所做的完全一样,只是将继承加入其中。为什么会出错?如果有帮助,我正在使用 Visual Studio 2017。
【问题讨论】:
标签: c++ templates visual-c++