c++ 编译报错汇总(随时更新)

1、invalid new-expression of abstract class type ‘×××ב

这个报错代表一个尝试在实例化一个抽象类,也就是说父类的接口中有纯虚函数在子类中没有实现;

举例:

//父类
class parent:
{
  virtual ~parent();
  virtual void func1() = 0;
};

//子类
class child: public parent
{
  child();
  ~child();
};

int main()
{
  parent *test = new child();
};

这样编译就会报错,子类中必须要实现所有父类里面定义的纯虚函数

正确方式如下:

class parent:
{
  virtual ~parent();
  virtual void func1() = 0;
};

class child: public parent
{
  child();
  ~child();
  void func1() {}
}

int main()
{
  parent *test = new child();
};

猜你喜欢

转载自www.cnblogs.com/Malphite/p/9903262.html