模板类定义中的模板构造函数定义

秃头

我试图像一个生成器类一样保存生成的类,两者都扩展了相同的类型。(在我的程序中它试图使虚拟化的通用思想)如下:

template <class T>
class V : public T {
    T& owner; // the T owner

    template <class... Args>
    explicit V(T &_owner, Args... args) : T(args...) {
        owner = _owner; // holds the owner
    }
}
...
int main() {
    type t = type(512);
    V<type> vt = V(t, 256); //ERROR: undefinied reference...(to constructor expanded)
}

但是在函数 main 中调用构造函数时出现该错误,我必须更改什么?我在 CLion IDE 中使用 C++17。

谢谢您的帮助

什莫

以下是对您的代码的一些修复:

template <class T>
class V : public T {
// needs to be public
public:
    T& owner;

    template <class... Args>
    explicit V(T &_owner, Args... args)
        : T(args...),
        // references need to be initialized here
        owner(_owner)
    { }
};

/// the super class
struct memory
{
    memory(int _i) : i(_i) {}
    int i;
};

int main() {
    memory m = memory(512);
    auto vm = V<memory>(m, 256);

    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

模板类中的函数定义

定义类模板构造函数并提供模板参数

如何在类模板之外定义类模板的构造函数模板?

如何在另一个类模板中定义完全专业的类的构造函数

“未定义的引用”模板类的构造函数

在模板类中定义模板化的朋友函数

基于模板变量参数的类模板中的函数指针定义

模板函数与类的脱机定义

类模板构造函数中对“Position<int>::treeHeight”的未定义引用

如何定义另一个模板类的内部模板类的构造函数?

在C ++类中构造自定义模板

类any中的模板构造函数与非模板构造函数

外部定义的模板类中的模板方法

在模板文字中定义函数

如何定义模板类的模板

如何定义专用模板类的成员函数?

在使用 SFINAE 的模板类之外定义函数?

成员函数的C ++条件模板类定义

定义模板化类函数的错误

在类模板中定义本地类的类型

非模板类的构造函数中的模板参数

在模板类中包装C ++模板构造函数

在模板化类之外定义非模板函数

使用成员函数中使用的两个模板在类模板中定义成员函数的单个模板

根据template参数有条件地定义模板类的构造函数

模板和隐式构造函数的类定义之外的Friend声明

在同一标头中定义但在类外部的模板构造函数未识别

g ++和clang ++的不同行为与在模板类中定义的朋友模板函数

在类模板中未定义参数的情况下调用好友模板函数