c++ - no appropriate default constructor available - iterator? -
i cant code compile. added in iterator design pattern , think cause of error: when click on error takes me class electricmenu constructor.. maybe virtual iterator in menu class causing it?
error c2512: 'guitars::composite::inventoryparts::menu' : no appropriate default constructor available
i have composite design pattern , tring incorporate iterator design pattern , maybe cause since maybe have wrong interface.
here code error originating. not doing in main yet, wont compile. include 1 class if thought culprit. trying keep short possible..sorry dont lose interest please
#ifndef _electric_menu_ #define _electric_menu_ #include "menu.h" #include "menuitem.h" #include "electricmenuiterator.h" namespace guitars { namespace composite { namespace inventoryparts { class electricmenu : public menu { private: static const int max_items = 6; int _numberofitems; menuitem** _menuitems; public: electricmenu() : _numberofitems( 0 ) // error takes me { _menuitems = new menuitem*[max_items + 1]; // added 1 additional entry; for( int = 0; <= max_items; i++ ) { // hold null ( 0 ) value _menuitems[i] = 0; // hasnext() work } additem( "electric","flying v", true, 2.99); } void additem( std::string name, std::string description, bool vegetarian, double price) { menuitem* menuitem = new menuitem(name, description, vegetarian, price); if( _numberofitems >= max_items) { std::cerr << "sorry, menu full! can't add item menu" << std::endl; } else { _menuitems[_numberofitems] = menuitem; _numberofitems++; } } menuitem** getmenuitems() const { return _menuitems; } iterator<menuitem>* createiterator() const { return dynamic_cast< iterator< menuitem >* >( new electricmenuiterator( _menuitems) ); } }; } } } #endif
iterator
#ifndef _electric_menu_iterator_ #define _electric_menu_iterator_ #include "iterator.h" namespace guitars { namespace composite { namespace inventoryparts { class electricmenuiterator : public iterator<menuitem> { private: menuitem** _items; mutable int _position; public: explicit electricmenuiterator(menuitem** items) : _items(items), _position( 0 ) { } menuitem* next() const { menuitem* menuitem = _items[_position]; _position++; return menuitem; } bool hasnext() const { if( _items[_position] == 0 ) { return false; } else { return true; } } void remove() { } }; } } } #endif
iterator
#ifndef _iterator_ #define _iterator_ namespace guitars { namespace composite { namespace inventoryparts { template <class t> class iterator { public: virtual bool hasnext() const = 0; virtual t* next() const = 0; virtual ~iterator() = 0 { } };
heres menu...
#ifndef _menu_ #define _menu_ #include "menucomponent.h" #include "inventoryitem.h" #include "iterator.h" #include <assert.h> #include <vector> #include "menuitem.h" namespace guitars { namespace composite { namespace inventoryparts { class menu : public menucomponent { private: std::string _name; std::string _description; mutable std::vector< menucomponent* > _menucomponents; public: virtual iterator<menuitem>* createiterator() const = 0; virtual ~menu() = 0 { } menu( const std::string name, const std::string description ) : _name( name ), _description( description ) { } void add( menucomponent* menucomponent ) { assert( menucomponent ); _menucomponents.push_back( menucomponent ); } void remove( menucomponent* menucomponent ) { assert( menucomponent ); //std::remove( _menucomponents.begin(), _menucomponents.end(), menucomponent ); } menucomponent* getchild( int ) const { return _menucomponents[i]; } std::string getname() const { return _name; } std::string getdescription() const { return _description; } void print() const { std::cout << std::endl << getname().c_str(); std::cout << ", " << getdescription().c_str() << std::endl; std::cout << "---------------------" << std::endl; std::vector< menucomponent* >::iterator iterator = _menucomponents.begin(); while( iterator != _menucomponents.end() ) { menucomponent* menucomponent = *iterator++; menucomponent->print(); } } }; } } }
thank taking time me.. sorry if long.
your menu
class doesn't have default constructor. 2 constructors implicitly declared copy constructor , user-declared constructor:
menu( const std::string name, const std::string description )
because of this, must explicitly initialize menu
base class subobject in initialization list of electricmenu
constructor.
electricmenu() : menu("name", "description"), _numberofitems( 0 )
alternatively, can declare default constructor menu
class; whether makes sense depends on how expect menu
used.
Comments
Post a Comment