• <small id='pP0m9'></small><noframes id='pP0m9'>

  • <legend id='pP0m9'><style id='pP0m9'><dir id='pP0m9'><q id='pP0m9'></q></dir></style></legend>

      <tfoot id='pP0m9'></tfoot>
      <i id='pP0m9'><tr id='pP0m9'><dt id='pP0m9'><q id='pP0m9'><span id='pP0m9'><b id='pP0m9'><form id='pP0m9'><ins id='pP0m9'></ins><ul id='pP0m9'></ul><sub id='pP0m9'></sub></form><legend id='pP0m9'></legend><bdo id='pP0m9'><pre id='pP0m9'><center id='pP0m9'></center></pre></bdo></b><th id='pP0m9'></th></span></q></dt></tr></i><div id='pP0m9'><tfoot id='pP0m9'></tfoot><dl id='pP0m9'><fieldset id='pP0m9'></fieldset></dl></div>
      • <bdo id='pP0m9'></bdo><ul id='pP0m9'></ul>

        C++ 对 vtable 和继承的未定义引用

        时间:2023-08-02
          <tbody id='s2MzQ'></tbody>

            <i id='s2MzQ'><tr id='s2MzQ'><dt id='s2MzQ'><q id='s2MzQ'><span id='s2MzQ'><b id='s2MzQ'><form id='s2MzQ'><ins id='s2MzQ'></ins><ul id='s2MzQ'></ul><sub id='s2MzQ'></sub></form><legend id='s2MzQ'></legend><bdo id='s2MzQ'><pre id='s2MzQ'><center id='s2MzQ'></center></pre></bdo></b><th id='s2MzQ'></th></span></q></dt></tr></i><div id='s2MzQ'><tfoot id='s2MzQ'></tfoot><dl id='s2MzQ'><fieldset id='s2MzQ'></fieldset></dl></div>

            <tfoot id='s2MzQ'></tfoot>

            <small id='s2MzQ'></small><noframes id='s2MzQ'>

              • <legend id='s2MzQ'><style id='s2MzQ'><dir id='s2MzQ'><q id='s2MzQ'></q></dir></style></legend>
                • <bdo id='s2MzQ'></bdo><ul id='s2MzQ'></ul>
                  本文介绍了C++ 对 vtable 和继承的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  文件 A.h

                  #ifndef A_H_#define A_H_A类{上市:虚拟 ~A();虚空 doWork();};#万一

                  文件 Child.h

                  #ifndef CHILD_H_#define CHILD_H_#include "A.h"类孩子:公共 A {私人的:整数 x,y;上市:孩子();~孩子();void doWork();};#万一

                  和 Child.cpp

                  #include "Child.h"孩子::孩子(){x = 5;}子::~子(){...}void Child::doWork(){...};

                  编译器说 A 有一个对 vtable 的未定义引用.我尝试了很多不同的东西,但都没有奏效.

                  我的目标是让 A 类成为一个接口,并将实现代码与标头分开.

                  解决方案

                  为什么会报错&如何解决?

                  您需要提供定义 用于 A 类 中的所有虚函数.只允许纯虚函数没有定义.

                  即:在 class A 两个方法:

                  虚拟~A();虚空 doWork();

                  应该被定义(应该有一个主体)

                  例如:

                  A.cpp

                  void A::doWork(){}A::~A(){}

                  <小时>

                  警告:
                  如果您希望 class A 充当接口(又名 抽象类(在 C++ 中),那么您应该使该方法纯虚拟.

                  virtual void doWork() = 0;

                  <小时>

                  好书:

                  虚拟表"是什么意思外部未解决?
                  在构建 C++ 时,链接器说我的构造函数、析构函数或虚拟表未定义.>

                  File A.h

                  #ifndef A_H_
                  #define A_H_
                  
                  class A {
                  public:
                      virtual ~A();
                      virtual void doWork();
                  };
                  
                  #endif
                  

                  File Child.h

                  #ifndef CHILD_H_
                  #define CHILD_H_
                  
                  #include "A.h"
                  
                  class Child: public A {
                  private:
                      int x,y;
                  public:
                      Child();
                      ~Child();
                      void doWork();
                  };
                  #endif
                  

                  And Child.cpp

                  #include "Child.h"
                  
                  Child::Child(){
                      x = 5;
                  }
                  
                  Child::~Child(){...}
                  
                  void Child::doWork(){...};
                  

                  The compiler says that there is a undefined reference to vtable for A. I have tried lots of different things and yet none have worked.

                  My objective is for class A to be an Interface, and to seperate implementation code from headers.

                  解决方案

                  Why the error & how to resolve it?

                  You need to provide definitions for all virtual functions in class A. Only pure virtual functions are allowed to have no definitions.

                  i.e: In class A both the methods:

                  virtual ~A();
                  virtual void doWork();
                  

                  should be defined(should have a body)

                  e.g.:

                  A.cpp

                  void A::doWork()
                  {
                  }
                  A::~A()
                  {
                  }
                  


                  Caveat:
                  If you want your class A to act as an interface(a.k.a Abstract class in C++) then you should make the method pure virtual.

                  virtual void doWork() = 0;
                  


                  Good Read:

                  What does it mean that the "virtual table" is an unresolved external?
                  When building C++, the linker says my constructors, destructors or virtual tables are undefined.

                  这篇关于C++ 对 vtable 和继承的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:访问另一个子类中基类的受保护成员 下一篇:为什么不从 std::allocator 继承

                  相关文章

                  最新文章

                  • <bdo id='N0DzE'></bdo><ul id='N0DzE'></ul>

                  <small id='N0DzE'></small><noframes id='N0DzE'>

                • <tfoot id='N0DzE'></tfoot>

                  <i id='N0DzE'><tr id='N0DzE'><dt id='N0DzE'><q id='N0DzE'><span id='N0DzE'><b id='N0DzE'><form id='N0DzE'><ins id='N0DzE'></ins><ul id='N0DzE'></ul><sub id='N0DzE'></sub></form><legend id='N0DzE'></legend><bdo id='N0DzE'><pre id='N0DzE'><center id='N0DzE'></center></pre></bdo></b><th id='N0DzE'></th></span></q></dt></tr></i><div id='N0DzE'><tfoot id='N0DzE'></tfoot><dl id='N0DzE'><fieldset id='N0DzE'></fieldset></dl></div>
                      <legend id='N0DzE'><style id='N0DzE'><dir id='N0DzE'><q id='N0DzE'></q></dir></style></legend>