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

发布于 2024-10-16 19:50:06 字数 4470 浏览 2 评论 0原文

我无法编译我的代码。我添加了迭代器设计模式,我认为这可能是导致我的错误的原因:当我单击错误时,它会将我带到 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();
    }
}
};

} 
} 
} 

谢谢您花时间帮助我......抱歉,如果太长了。

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.

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

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

发布评论

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

评论(1

鹿港巷口少年归 2024-10-23 19:50:06

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

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

因此,您必须在 ElectricMenuMenu 基类子对象> 构造函数。

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

或者,您可以为 Menu 类声明一个默认构造函数;这是否有意义取决于您期望如何使用 Menu

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.

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