Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3931334
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:20:31+00:00 2026-05-19T23:20:31+00:00

I cant get my code to compile. I added in a iterator design pattern

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:20:31+00:00Added an answer on May 19, 2026 at 11:20 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the error I'm getting when trying to compile some code that uses
Can't get to my site. Apache gives the following error message: [Fri Sep 05
I think this must be simple but I can't get it right... I have
This sounds dumb, but I can't get it to work. I think i just
I'm having trouble with something that I thought would be easy... I can't get
I have a C# application (project A) that requires X.dll. I have added the
I'm trying to compile the following snippet of code: #include <boost/filesystem/path.hpp> //Other includes snipped
I am trying to pass a dynamic variable and I cant get it to
I can't get my GridView to enable a user to sort a column of
So this seems pretty basic but I can't get it to work. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.