小编wuw*_*bin的帖子

具有协变返回类型和模板类参数的虚拟继承,vs2013中的LINK错误

这是我的代码:

#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({});
}
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

我试过gcc,然后编译.

如果我执行以下任何一项操作,VC将编译:

  1. 将第(1)行更改为非模板类​​型
  2. 删除行中的"虚拟"(2) …

c++ virtual templates covariance visual-c++

8
推荐指数
1
解决办法
424
查看次数

标签 统计

c++ ×1

covariance ×1

templates ×1

virtual ×1

visual-c++ ×1