tripathisachin Ответов: 1

Я испытываю проблему [error: invalid new-expression of abstract class type] при написании кода на C++ ?


пожалуйста, предоставьте мне наилучшее возможное решение этой проблемы.
/**************************
* File Implementation of Factory Design Pattern In an Instumetation Requrement ************************************/

#include <iostream>

using namespace std;

enum InstrumentType 
 {
     IT_INPUT=1, IT_OUTPUT 
 };

enum Dmm_VendorType
 {
     DMM_TACTRONICS=1, DMM_AGILENT 
 };

enum Pwm_VendorType 
{
     PWM_CHROMA=1, PWM_AGILENT 
};

///////////////////////////************************************** Library Calsses *****************************************/////////////////////////////////
class Instrument 
{
      public:
     
     virtual void Instrument_type() = 0;
     static Instrument* Create_instrument(int type_in);
};

class Input_Instrument : public Instrument
{
    public:
         virtual void Measurment_Voltage() = 0;
         static Input_Instrument* Create_Dmm(int type_in);
         static Input_Instrument* Create_Pwm(int type_in);
    
     void Instrument_type()
      {
        cout<<"You are in Input Instrument section...."<<endl;
      }
};

class Dmm_tactronics : public Input_Instrument
{

    public:
        void Measurment_Voltage()
        {
            cout << "This is Tactronic Dmm voltage...." <<endl;
        }
};

class Dmm_agilent : public Input_Instrument
{
    public:
        void Measurment_Voltage()
         {
             cout << "This is Agilent Dmm voltage...." <<endl;
         }
  };

class Power_Meter_Chroma : public Input_Instrument
{     
    public:
       void Measurment_Voltage()
         {
              cout<<"This is chroma Power_Meter voltage....."<<endl;
         }
};

class Power_Meter_agilent : public Input_Instrument
{     
    public:    
      void Measurment_Voltage()
       {
           cout<<"This is Agilent Power_Meter voltage....."<<endl;
       }    
};

class Output_instrument : public Instrument
{
     public:
      void Instrument_type()
        {
            cout<<"You are Output instrument section...."<<endl;
        }
};

// Factory method to create objects of different types.
// Change is required only in this function to create a new object type

/***************************** For Instrument Name Selection **************************************/

Instrument* Instrument::Create_instrument(int type_in) 
{
    if (type_in == IT_INPUT)
    return new Input_Instrument();
    else if (type_in == IT_OUTPUT)
    return new Output_instrument();
 
    else 
    return NULL;
}

/***************************** For DMM Vendor Name Selection **************************************/

Input_Instrument* Input_Instrument::Create_Dmm(int type_in) 
{
       if (type_in == DMM_TACTRONICS)
       return new Dmm_tactronics();
       else if (type_in == DMM_AGILENT)
       return new Dmm_agilent();
 
       else 
       return NULL;
}

/***************************** For Power meter Vendor Name Selection **************************************/
Input_Instrument* Input_Instrument::Create_Pwm(int type_in) 
{    
    if (type_in == PWM_CHROMA)
    return new Power_Meter_Chroma();
    else if (type_in == PWM_AGILENT)
    return new Power_Meter_agilent();
 
    else 
    return NULL;
}

/////////////////////////************************************** Client Calsses *****************************************////////////////////////////////

// Client doesn't explicitly create objects
// but passes type to factory method Create_instrument(),Create_Dmm(),Create_Pwm().
    
class client_Instrument
{
     public:
     client_Instrument()
     {
          cout<<"Enter Instrument type....."<<endl;
          cin >>type_in ;
          pInstrument = Instrument::Create_instrument(type_in);
     }
     ~client_Instrument() 
        {
            if (pInstrument) 
              {
                  delete[] pInstrument;
                  pInstrument = NULL;
              }
        }
    
        Instrument* getInstrumentName()  
        {
             return pInstrument;
        }
             
      private:
          Instrument *pInstrument;
          int type_in;
};

class client_DMM 
{
    public:
        client_DMM()
         {
             cout<<"Select Dmm Vendor type....."<<endl;
             cin >>type_in ;
             pInput_DMM_Instrument = Input_Instrument::Create_Dmm(type_in);
         }
    
       ~client_DMM() 
          {
              if (pInput_DMM_Instrument) 
                {
                     delete[] pInput_DMM_Instrument;
                     pInput_DMM_Instrument = NULL;
                 }
           }
    
        Input_Instrument* get_DMM_InstrumentName()  
        {
             return pInput_DMM_Instrument;   
        }
              
     private:
    
       Input_Instrument *pInput_DMM_Instrument;
       int type_in;
};

class client_PWM 
{
    public:
          client_PWM()
          {
                cout<<"Select PWM Vendor type....."<<endl;
                cin >>type_in ;
                pInput_PWM_Instrument = Input_Instrument::Create_Pwm(type_in);
           }
    
         ~client_PWM() 
           {          
                 if (pInput_PWM_Instrument) 
                   {
                        delete[] pInput_PWM_Instrument;
                        pInput_PWM_Instrument = NULL;
                    }    
            }
            
          Input_Instrument* get_PWM_InstrumentName()  
             {
                  return pInput_PWM_Instrument;
             }

    private:
         Input_Instrument *pInput_PWM_Instrument;
          int type_in;
};

// Driver program
 int main() 
 {
             client_Instrument *pclient_Instrument = new client_Instrument();
    
             Instrument * pInstrument = pclient_Instrument->getInstrumentName();
    
             pInstrument->Instrument_type();
    
             client_DMM *pclient_DMM = new client_DMM();
    
             Input_Instrument * pInput_DMM_Instrument = pclient_DMM->get_DMM_InstrumentName();
    
             pInput_DMM_Instrument->Measurment_Voltage();
    
             client_PWM *pclient_PWM = new client_PWM();
    
             Input_Instrument * pInput_PWM_Instrument = pclient_PWM->get_PWM_InstrumentName();
    
             pInput_PWM_Instrument->Measurment_Voltage();
    
             return 0;
 }


Что я уже пробовал:

Я пытаюсь реализовать шаблон Заводского проектирования и загрузить дочерний класс через новое ключевое слово в ответ, что дочерний класс на самом деле содержит чистое определение виртуальной функции.

1 Ответов

Рейтинг:
12

KarstenK

Класс с чистой виртуальной функцией не может быть создан. Вы должны использовать дочерний класс С это переопределенная функция.

Некоторые дальнейшие объяснения вы найдете здесь.

Узнайте о концепциях C++ и переосмыслите эту функцию с точки зрения типа возвращаемого значения и использования.