我正在类中创建一个虚拟克隆方法,并将在主文件中进行演示。但是,当我尝试这样做时,会出现错误。

这是我的课:

// includes

#include <string>
#include <exception>
#include <sstream>

namespace Vehicle_Renting {

   using namespace std;

    class Auto_Rent_Exception : public std::exception{
       protected:
          string error;
       public:
          Auto_Rent_Exception(){
          }

          virtual const string what() = 0;
          virtual ~Auto_Rent_Exception() throw();
          virtual Auto_Rent_Exception* clone() = 0;
    };

    class short_input : public Auto_Rent_Exception{
    public:
      short_input(const string errorMsg){
        stringstream ss;
        ss << errorMsg << ": short_input" << endl;
        error = ss.str();
      }
      virtual const string what(){
         return error;
      }
      virtual short_input* clone(){
         return new short_input(*this);
      }
    };
}


这是我的使用方式:

cout << "Virtual Clone" << endl;
Auto_Rent_Exception* exc = new short_input("Smthing");   //Original copy
Auto_Rent_Exception* copy;
copy = exc->clone();
cout << exc->what();
cout << copy->what() << endl;
delete exc;


我收到这些我不知道如何修复的错误:

undefined reference to `vtable for Vehicle_Renting::Auto_Rent_Exception'
undefined reference to `Vehicle_Renting::Auto_Rent_Exception::~Auto_Rent_Exception()'
undefined reference to `vtable for Vehicle_Renting::Auto_Rent_Exception'
undefined reference to `Vehicle_Renting::Auto_Rent_Exception::~Auto_Rent_Exception()'
undefined reference to `Vehicle_Renting::Auto_Rent_Exception::~Auto_Rent_Exception()'


Link to error image

最佳答案

您实际上需要一个析构函数:

virtual ~Auto_Rent_Exception()
{
}

09-07 04:37