Search code examples
c++design-patternsvirtual

no appropriate default constructor available - iterator?


I cant get my code to compile. I added in a iterator design pattern and i think that could be the cause of my error: when i click on the error it takes me to the class ElectricMenu constructor.. maybe the virtual iterator in the menu class is causing it?

error C2512: 'guitars::Composite::InventoryParts::Menu' : no appropriate default constructor available

I have the composite design pattern and i am tring to incorporate iterator design pattern and maybe that the cause since maybe i have a wrong interface.

here is the code where the error is originating. I am not doing anything in main yet, it just wont compile. I would just include that one class if i thought that was the culprit. I am trying to keep this short as 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 )           // this is where the error takes me
        {                               
    _menuItems = new MenuItem*[MAX_ITEMS + 1];  // added one additional entry;
    for( int i = 0; i <= MAX_ITEMS; i++ ) {     // to hold a null ( 0 ) value
        _menuItems[i] = 0;                      // so hasNext() will 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 is full!  Can't add item to 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 the 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 i ) 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 you for taking the time to help me.. sorry if its too long.


Solution

  • Your Menu class doesn't have a default constructor. Its only two constructors are the implicitly declared copy constructor and your user-declared constructor:

    Menu( const std::string name, const std::string description )
    

    Because of this, you must explicitly initialize the Menu base class subobject in the initialization list of your ElectricMenu constructor.

    ElectricMenu() : Menu("name", "description"), _numberOfItems( 0 )
    

    Alternatively, you can declare a default constructor for the Menu class; whether this makes sense depends on how you expect Menu to be used.