我试图像一个生成器类一样保存生成的类,两者都扩展了相同的类型。(在我的程序中它试图使虚拟化的通用思想)如下:
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] 删除。
我来说两句