c++ - 基类构造函数错误

标签 c++ compiler-errors derived-class base-class

<分区>

Possible Duplicate:
What is this weird colon-member syntax in the constructor?

我有以下基类和派生类;

class P {
 int n;
 public:
  P( int id );
  virtual  int getn();
  virtual  int toss( int x ) = 0; 
};

class RNP : public P {
  int n;
 public:
    RNP(  int id);
    int toss( int x );
};

我已经为 RNP 创建了一个构造函数,但是当我编译时出现错误

player.cc:9:11: error: constructor for 'RNP' must explicitly initialize the base class 'P' which does not have a default constructor

如何在派生类中初始化基类?

最佳答案

只需调用其构造函数即可。它可以在初始化列表中完成,您可以在其中定义 RNP::RNP:

RNP::RNP( int id )
:
    P( id )
{
    //...
}

关于c++ - 基类构造函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13346197/

相关文章:

c++ - 如何正确地从嵌套结构派生?

C# 派生类不会覆盖基方法

c++ - 如何使用线程处理标准容器?

c++ - 在 C++ 中嵌入 Mongoose Web 服务器

c++ - 计算表示稀疏 vector 的 map 之间的距离 C++

c++ - VS2013 Variadic 模板编译错误

c# - 将派生类作为基类传递给 Web 服务方法

c++ - boost 日志 : Interaction of log settings file and code configuration

visual-studio - cl(Windows Visual Studio编译器)是否应该在任何命令提示符下工作?

java - 可能的 eclipse 错误