简体   繁体   English

C ++没有合适的默认构造函数-没有参数的继承的模板构造函数

[英]C++ no appropriate default constructor available - inherited templated constructor w/ no arguments

I've looked at over a dozen qst's with the phrase no appropriate default constructor available in it but none help with my own problem. 我已经看过十几个qst,其中no appropriate default constructor available ,但是没有解决我自己的问题的方法。

It's a very basic C++ qst (as I'm still learning the ropes), so sorry in advance if your eyes are glazing over at it's simplicity. 这是一个非常基本的C ++ qst(因为我仍在学习技巧),所以如果您的视线不那么简单,请提前对不起。 I am trying to inherit a class that has a templated constructor with no arguments. 我试图继承一个具有模板构造函数且没有参数的类。 Such as: 如:

class Base {
public:

template<class T>
Base() {
  T *t = this;
  //more irrelevant stuff
}

}

I've tried 我试过了

class Derived : public Base {
public:
  Derived() : Base() {

  }
}

and

class Derived : public Base {
public:
  Derived() {

  }
}

to no avail. 无济于事。 In both cases I get the error message no appropriate default constructor available . 在这两种情况下,我都会收到错误消息, no appropriate default constructor available How to go about doing this? 如何去做呢? If this is not possible can you explain why? 如果这不可能,您能解释为什么吗?

It worked when I set up my constructor like template<class T> Base(T *t) (taking the template argument as a parameter). 当我设置诸如template<class T> Base(T *t)构造函数时(以template参数作为参数),它起作用了。

ps In case it matters, in my code I am also inheriting Derived by another class. ps如果很重要,在我的代码中,我还将继承另一个类的Derived

there is no syntax for accessing the default constructor in 没有用于访问默认构造函数的语法

class blah_t
{
public:
    template< class other_t > blah_t() {}
};

there also some other ways to make the default constructor inaccessible, eg 还有一些其他方法可以使默认构造函数不可访问,例如

class blah_t
{
public:
    blah_t() {}
    blah_t( int = 666 ) {}
};

as the holy standard explains it, constructors don't have names… 正如神圣的标准所解释的那样,构造函数没有名称...

but, back to the original question, in order to be able to specify the template argument, you need some ordinary function argument that involves the template parameter type 但是,回到原始问题,为了能够指定模板参数,您需要一些涉及模板参数类型的普通函数参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM