没有合适的默认构造函数 - 迭代器?

时间:2011-02-09 03:50:04

标签: c++ design-patterns virtual

我无法编译代码。我添加了一个迭代器设计模式,我认为这可能是我的错误的原因:当我点击错误它带我到类ElectricMenu构造函数..也许菜单类中的虚拟迭代器导致它?

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

我有复合设计模式,我想要合并迭代器设计模式,也许是因为我可能有错误的界面。

这是发生错误的代码。我还没有在main中做任何事情,它只是不会编译。 如果我认为那是罪魁祸首,我会把这一类包括在内。我试图尽可能保持这种简短。不要失去兴趣

#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

迭代

#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

迭代

#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 {
}


};

继承人的菜单......

#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();
    }
}
};

} 
} 
} 

感谢您抽出宝贵时间帮助我..抱歉,如果太长了。

1 个答案:

答案 0 :(得分:7)

您的Menu类没有默认构造函数。它只有两个构造函数是隐式声明的复制构造函数和用户声明的构造函数:

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

因此,您必须在Menu构造函数的初始化列表中显式初始化ElectricMenu基类子对象。

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

或者,您可以为Menu类声明默认构造函数;这是否有意义取决于您期望Menu的使用方式。