不能分配抽象类型的对象

cannot allocate an object of abstract type

本文关键字:类型 对象 抽象类 抽象 不能分 分配 不能      更新时间:2023-10-16

在下面的代码片段中,我在添加纯虚函数createImage时出现以下错误GUIFactory 类。

错误:无法分配抽象类型为"工厂"的对象

这里的工厂不是一个抽象类,所以理想情况下它应该是可实例化的。除此之外,如果删除此功能并且只添加了纯虚拟创建按钮,那么我没有收到此错误,这似乎是有效的行为。

法典:

#include <iostream>
#include<string.h>
using namespace std;
class Button {
public:
    virtual void paint() = 0;
};
class Image {
public:
        virtual void paint() = 0;
};
class OSXButton: public Button {
public:
    void paint() {
        std::cout << "OSX button n";
    }
};
class WindowsButton: public Button  {
public:
    void paint() {
        std::cout << "Windows button n";
    }
};
class iPhoneButton: public Button {
public:
    void paint() {
        std::cout << "iPhone button n";
    }
};
class OSXImage: public Image {
public:
        void paint() {
                std::cout << "OSX button n";
        }
};
class WindowsImage: public Image  {
public:
        void paint() {
                std::cout << "Windows button n";
        }
};
class iPhoneImage: public Image {
public:
        void paint() {
                std::cout << "iPhone button n";
        }
};

class GUIFactory {
public:
    virtual Button *createButton(const char *) = 0;
       virtual Image *createImage(const char *) = 0;//If this function is commented code works fine. 
};
class Factory: public GUIFactory {
public:
    Button *createButton(const char *type) {
        if(strcmp(type,"Windows") == 0) {
            return new WindowsButton;
        }
        else if(strcmp(type,"OSX") == 0) {
            return new OSXButton;
        }
                else if(strcmp(type,"iPhone") == 0) {
                        return new iPhoneButton;
                }
                else
                {
              return NULL;
                }


    }
#if 0
Image *createImage(const char *type) {
                if(strcmp(type,"Windows") == 0) {
                        return new WindowsImage;
                }
                else if(strcmp(type,"OSX") == 0) {
                        return new OSXImage;
                }
                else if(strcmp(type,"iPhone") == 0) {
                        return new iPhoneImage;
                }
                else
                {
              return NULL;
                }


        }
#endif

};
int main()
{
    GUIFactory* guiFactory;
    Button *btn;
        Image *img;
    guiFactory = new Factory;
    btn = guiFactory->createButton("OSX");
        if(btn!=NULL)
        {
    btn -> paint();
        }
    btn = guiFactory->createButton("Windows");
        if(btn!=NULL)
        {
        btn -> paint();
        }
        btn = guiFactory->createButton("iPhone");
        if(btn!=NULL)
        {
        btn-> paint();
        }
        #if 0
        img = guiFactory->createImage("OSX");
        if(img!=NULL)
        {
        img -> paint();
        }
        img = guiFactory->createImage("Windows");
        if(img!=NULL)
        {
        img -> paint();
        }
        img = guiFactory->createImage("iPhone");
        if(img!=NULL)
        {
        img -> paint();
        }


        //btn -> paint();
        #endif
        delete guiFactory;
       // delete Button;
    return 0;
}

删除#if 0并围绕createImage的定义#endif

它们的存在消除了定义,因此Factory不会覆盖纯虚函数,因此仍然是抽象的。