我尝试从类模板“ TControl”继承,但是派生类“ TControlML”看不到基类的构造函数。我已阅读相关文章,但仍未找到原因。我做了一个最小的例子:

using namespace std;

class MeshLink
{
};


template<typename WU_TYPE>
class TControl
{
public:
    TControl(std::vector<WU_TYPE>& vWorkunits_):
        sTodo(vWorkunits_.begin(),vWorkunits_.end())
    {}
    std::set<WU_TYPE> sTodo;
};



class TControlML:public TControl<MeshLink*>
{
public:

};


int main()
{
    vector<MeshLink*> vMeshLinks;
    TControl<MeshLink*> ctrl(vMeshLinks); // Good
    TControlML ctrl2(vMeshLinks); // Fails

}


海湾合作委员会说:

test.cpp:35:29: error: no matching function for call to ‘TControlML::TControlML(std::vector<MeshLink*, std::allocator<MeshLink*> >&)’
  TControlML ctrl2(vMeshLinks); // Fails

最佳答案

您必须使用基本构造函数,您可以使用using

class TControlML:public TControl<MeshLink*>
{
public:
    using TControl::TControl;
};


还是旧方法:

class TControlML:public TControl<MeshLink*>
{
public:
    TControlML(std::vector<WU_TYPE>& vWorkunits_):TControl(vWorkunits_) {}
    // Same for each constructor
};

07-26 05:04