为什么我收到错误“错误 C2259:...无法实例化抽象类”?

发布于 2024-10-17 01:35:32 字数 6812 浏览 3 评论 0原文

任何帮助都是appriced。我正在研究 C++ 工厂模式,但收到此错误。

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

这是我的代码:

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

任何帮助都会很好,谢谢时间...=)

Any help is appriciated. I'm working on a C++ factory pattern and i get this error.

1>c:\users\brian\documents\visual studio 2010\projects\cst276lab_3\guitar.hpp(456): error C2259: 'ElectricGuitarComponentFactory' : cannot instantiate abstract class

This is my code:

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

any help would be nice thanks ahead of time...=)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仄言 2024-10-24 01:35:32

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

此外,您使用的语法格式

public: virtual ~GuitarComponentFactory() = 0 {}

不正确。您不能声明纯虚函数并在类定义中为其提供定义。如果要提供纯虚函数的定义,则必须在类定义之外进行。您使用的编译器 Visual C++ 对于此规则有点宽松。

To fix the specific problem, you need to declare a destructor for ElectricGuitarComponentFactory because you have declared the destructor of the base class as a pure virtual function. Why you have declared the base class destructor as pure virtual, I don't know; it really doesn't make any sense to do that. The destructor should be declared virtual, but not as pure virtual.

Also, the syntax you have used,

public: virtual ~GuitarComponentFactory() = 0 {}

is ill-formed. You cannot declare a pure virtual function and provide a definition for it in the class definition. If you want to provide a definition for a pure virtual function, you must do so outside of the class definition. The compiler you are using, Visual C++, is a bit lenient with respect to this rule.

内心旳酸楚 2024-10-24 01:35:32
class GuitarComponentFactory 
  {
     public: virtual ~GuitarComponentFactory() = 0 {}
  };

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

class GuitarComponentFactory 
  {
     public: virtual ~GuitarComponentFactory() = 0 {}
  };

In your derived ElectricGuitarComponentFactory you don't provide a destructor, hence it's still an abstract class.

烙印 2024-10-24 01:35:32

ElectricGuitarComponentFactory继承GuitarComponentFactory ....

当你继承一个抽象类(abstract=具有纯虚方法)时,那么你需要重写所有这些方法。

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

。您忘记重写析构函数。

ElectricGuitarComponentFactory inherits GuitarComponentFactory ....

When you inherit an abstract class (abstract = has pure virtual methods), then you need to override all those methods.

in GuitarComponentFactory you got 7 methods, and in ElectricGuitarComponentFactory you only declared 6.

You forgot to override the destructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文