首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我收到错误消息"error C2259:... cannot instantiate abstract“?

为什么我收到错误消息"error C2259:... cannot instantiate abstract“?
EN

Stack Overflow用户
提问于 2011-02-14 14:38:06
回答 3查看 8.9K关注 0票数 0

任何帮助都是有价值的。我正在处理一个C++工厂模式,我得到了这个错误。

1>c:\users\brian\documents\visual studio 2010\projects\cst276lab_3\guitar.hpp(456):错误C2259:'ElectricGuitarComponentFactory‘:无法实例化抽象类

这是我的代码:

代码语言:javascript
复制
///////////////////////guitar class////////////////////

class Guitar

{

      private: std::string _name;

      protected: mutable std::auto_ptr< HeadStock > _HeadStock;
      protected: mutable std::auto_ptr< NeckStrap > _NeckStrap;
      protected: mutable std::vector< Bridge* > _Bridge;
      protected: mutable std::auto_ptr< Strings > _Strings;
      protected: mutable std::auto_ptr< Switches > _Switches;
      protected: mutable std::auto_ptr< GuitarBody > _GuitarBody;

      public: virtual void prepare() const = 0;

      private: Guitar( const Guitar& ); // Disable copy constructor
      private: void operator=( const Guitar& ); // Disable assignment operator

      protected: Guitar()
      {
      }
      public: virtual ~Guitar()
      {

         for( std::vector< Bridge* >::iterator iterator = _Bridge.begin();
         _Bridge.end() != iterator; ++iterator )
         {
            delete *iterator;
         }
         _Bridge.clear();

      }
      public: virtual void bake() const 
      {
         std::cout << "Bake for 25 minutes at 350" << std::endl;
      }
      public: virtual void cut() const 
      {
         std::cout << "Cutting the pizza into diagonal slices" << std::endl;
      }
      public: virtual void box() const
      {
         std::cout << "Place pizza in official PizzaStore box" << std::endl;
      }
      public: void setName( std::string name) 
      {
         _name = name;
      }
      public: std::string getName() const 
      {
         return _name;
      }
      public: std::string toString() const 
      {
    std::stringstream value; 
    value << "---- " << _name.c_str() << " ----" << std::endl;
    if( _HeadStock.get() != 0 ) 
    {
       value << _HeadStock->toString();
       value << std::endl;
         }
    if( _NeckStrap.get() != 0 ) 
    {
       value << _NeckStrap->toString();
       value << std::endl;
    }
    if( _Strings.get() != 0 ) 
    {
       value << _Strings->toString();
       value << std::endl;
    }
    if( _GuitarBody.get() != 0 )
    {
       value << _GuitarBody->toString();
       value << std::endl;
    }

    if( _Switches.get() != 0 )
    {
       value << _Switches->toString();
       value << std::endl;
    }

    if( _Bridge.size() != 0 )
    {
       for( std::vector< Bridge* >::iterator iterator = _Bridge.begin(); 
            _Bridge.end  () != iterator; ++iterator ) 
       {
          value << ( *iterator )->toString() << ", ";
       }
       value << std::endl;
    }

    return value.str();
      }
};


//////////////////////////////////////Class guitar store////////////////


class GuitarStore 

{

      protected: GuitarStore() 
      {
      }
      public: virtual ~GuitarStore() = 0 
      {
      }
      public: std::auto_ptr< Guitar > orderGuitar( std::string type ) const 
      {
         std::auto_ptr< Guitar > guitar( createGuitar( type ) );
         std::cout << "--- Making a " << guitar->getName() << " ---" << std::endl;
         guitar->prepare();
         guitar->bake();
         guitar->cut();
         guitar->box();
         return guitar;
      }
      public: virtual std::auto_ptr< Guitar > createGuitar( std::string type ) const = 0;
};


//////////////////////////////////guitar component factory////////////////////////////////



class GuitarComponentFactory 

      {
         public: virtual HeadStock* createHeadStock() const = 0;
         public: virtual NeckStrap* createNeckStrap() const = 0;
         public: virtual Strings* createStrings() const = 0;
         public: virtual std::vector< Bridge* > createBridge() const = 0;
         public: virtual Switches* createSwitches() const = 0;
         public: virtual GuitarBody* createGuitarBody() const = 0;
         public: virtual ~GuitarComponentFactory() = 0 {}
      };


///////////////////////////// electric guitar///////////////////

class ElectricGuitar : public Guitar

{

      private: mutable std::auto_ptr< GuitarComponentFactory > _ingredientFactory;

      public: explicit ElectricGuitar( GuitarComponentFactory* ingredientFactory ) :
      _ingredientFactory( ingredientFactory ) 
      {
         assert( ingredientFactory );
      }
      public: void prepare() const 
      {
         std::cout << "Preparing " << getName().c_str() << std::endl;
         _HeadStock = std::auto_ptr< HeadStock>( _ingredientFactory->createHeadStock() );
         _NeckStrap = std::auto_ptr< NeckStrap>( _ingredientFactory->createNeckStrap() );
         _Strings = std::auto_ptr< Strings>( _ingredientFactory->createStrings() );
         _Switches= std::auto_ptr< Switches>( _ingredientFactory->createSwitches() );
         if( _Bridge.empty() ) 
            _Bridge = _ingredientFactory->createBridge();
      }
};


//////////// electric guitar component factory////////////////


class ElectricGuitarComponentFactory : public GuitarComponentFactory 

{

      public: HeadStock* createHeadStock() const 
      {
         return new AngledHeadStock();
      }
      public: NeckStrap* createNeckStrap() const 
      {
         return new LeatherNeckStrap();
      }
      public: Strings* createStrings() const 
      {
         return new NylonStrings();
      }
      public: std::vector< Bridge* > createBridge() const 
      {
         std::vector< Bridge* > bridge;
         bridge.push_back( new ChromeBridge() );
         return bridge;
      }

      public: Switches* createSwithes() const
      {
         return new SPDT_Switches();
      }

      public: GuitarBody* createGuitarBody() const 
      {
         return new HollowGuitarBody();
      }

};


//////////////////// electric guitar srore  ////////////////


class ElectricGuitarStore : public GuitarStore 

{

      public: std::auto_ptr< Guitar > createGuitar( std::string item ) const
      {
         std::auto_ptr< Guitar > guitar( 0 );
         GuitarComponentFactory* ingredientFactory = new ElectricGuitarComponentFactory();
         if( item.compare( "Electric" ) == 0 ) 
         {
       guitar = std::auto_ptr< Guitar >( new ElectricGuitar( ingredientFactory ) );
       guitar->setName( "Electric GuitarBody" );
            return guitar;
         }
      }
};

任何帮助都将是很好的感谢在time...=之前)

EN

回答 3

Stack Overflow用户

发布于 2011-02-14 14:45:04

要修复特定问题,您需要为ElectricGuitarComponentFactory声明析构函数,因为您已将基类的析构函数声明为纯虚函数。我不知道为什么你要声明基类析构函数是纯虚的;这样做真的没有任何意义。析构函数应该声明为虚拟的,但不是纯虚拟的。

另外,您使用的语法,

代码语言:javascript
复制
public: virtual ~GuitarComponentFactory() = 0 {}

是病态的。不能声明纯虚函数并在类定义中为其提供定义。如果要为纯虚函数提供定义,则必须在类定义之外执行此操作。你正在使用的编译器,Visual C++,对这条规则有点宽松。

票数 4
EN

Stack Overflow用户

发布于 2011-02-14 14:42:49

代码语言:javascript
复制
class GuitarComponentFactory 
  {
     public: virtual ~GuitarComponentFactory() = 0 {}
  };

在派生的ElectricGuitarComponentFactory中,您没有提供析构函数,因此它仍然是一个抽象类。

票数 2
EN

Stack Overflow用户

发布于 2011-02-14 14:43:52

ElectricGuitarComponentFactory继承了GuitarComponentFactory ....

当你继承一个抽象类(抽象=有纯虚方法)时,你需要覆盖所有这些方法。

GuitarComponentFactory中你有7个方法,而在ElectricGuitarComponentFactory中你只声明了6个。

你忘了重写析构函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4989480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档