2

我已经查看了十几个带有短语的 qst no appropriate default constructor available,但对我自己的问题没有任何帮助。

这是一个非常基本的 C++ qst(因为我仍在学习绳索),如果您对它的简单性感到困惑,请提前抱歉。我正在尝试继承一个具有不带参数的模板化构造函数的类。如:

class Base {
public:

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

}

我试过了

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

  }
}

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

  }
}

无济于事。在这两种情况下,我都会收到错误消息no appropriate default constructor available。如何去做这件事?如果这是不可能的,你能解释为什么吗?

当我设置我的构造函数时它起作用了template<class T> Base(T *t)(将模板参数作为参数)。

p.s. In case it matters, in my code I am also inheriting Derived by another class.

4

1 回答 1

0

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, e.g.

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

于 2013-04-07T20:20:19.340 回答