【发布时间】:2014-11-20 10:38:23
【问题描述】:
这是我的代码:
#include <vector>
struct A
{
typedef std::vector<int> vec; //(1) template type
virtual A& test(vec) = 0;
};
struct B : public virtual A //(2) virtual inheritance
{
virtual B& test(vec) override //(3) covariant return type
{
return *this;
}
};
//std::vector<int> vv, cc(vv); //(4) explicit instantiate copy-ctor
int main()
{
B b;
b.test({});
}
Visual C++ 2013 给了我一个链接错误。
error LNK2001: unresolved external symbol "public: __thiscall
std::vector<int,class std::allocator<int> >::vector<int,class
std::allocator<int> >(class std::vector<int,class
std::allocator<int> > const &)"
(??0?$vector@HV?$allocator@H@std@@@std@@QAE@ABV01@@Z)
我尝试了 gcc,它可以编译。
如果我执行以下任一操作,VC 将编译:
- 将第(1)行改为非模板类型
- 去掉第(2)行中的“virtual”
- 将第(3)行的返回类型改为A&
- 取消注释第 (4) 行
为什么?
【问题讨论】:
-
直播测试链接:rextester.com/XZA77022
-
看起来像一个 VC 错误。
-
当您怀疑存在错误时,您应该提供准确的编译器版本。 VC++ 2013 不是一个确切的版本,有 4 个更新(如服务包)和特定的错误修复。您应该通过打开 Visual Studio 工具命令提示符并键入
cl来获得完整版本
标签: c++ templates visual-c++ virtual covariance