簡體   English   中英

沒有合適的默認構造函數-迭代器?

[英]no appropriate default constructor available - iterator?

我無法編譯我的代碼。 我添加了一個迭代器設計模式,我認為這可能是導致我的錯誤的原因:當我單擊該錯誤時,它將帶我到ElectricMenu類構造函數..也許菜單類中的虛擬迭代器導致了它?

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

我有復合設計模式,並且我打算合並迭代器設計模式,也許原因可能是因為我的接口錯誤。

這是導致錯誤的代碼。 我還沒有做任何主要的事情,它只會編譯。 如果我以為是罪魁禍首,我只想上一堂課。 我正在盡量保持簡短。對不起,請不要失去興趣。

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

} 
} 
} 

感謝您抽出寶貴的時間來幫助我。

您的Menu類沒有默認的構造函數。 它僅有的兩個構造函數是隱式聲明的副本構造函數和用戶聲明的構造函數:

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

因此,您必須在ElectricMenu構造函數的初始化列表中顯式初始化Menu基類的子對象。

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

另外,您可以為Menu類聲明一個默認的構造函數。 是否有意義取決於您希望Menu使用方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM