我在用C ++编写代码时遇到[错误:抽象类类型的新表达式无效]问题? [英] I am experiencing [error: invalid new-expression of abstract class type] problem while writing a code in C++ ?

查看:373
本文介绍了我在用C ++编写代码时遇到[错误:抽象类类型的新表达式无效]问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请为我提供该问题的最佳解决方案.

kindly provide me the best possible solution of the problem.

/**************************
* 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;
 }



我尝试过的事情:

我试图实现工厂设计模式,并通过返回新关键字加载子类,该子类实际上包含一个纯虚函数定义.



What I have tried:

I am trying to implement factory design pattern , and loading child class through new key word in return, that child class actually contain a pure virtual function definition.

推荐答案

一个类具有纯虚函数的实例不能被实例化.您必须使用子类 WITH 此重写函数.

您可以在此处找到一些进一步的解释.

了解C ++的概念,并在返回类型和用法中重新考虑该函数.
A class with pure virtual function cant be instanciated. You must use a child class WITH this overriden function.

Some further explanation you will find here.

Learn about the concepts of C++ and rethink that function in return type and usage.


这篇关于我在用C ++编写代码时遇到[错误:抽象类类型的新表达式无效]问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆