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 3405354
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:29:43+00:00 2026-05-18T05:29:43+00:00

I hope I got the relevant code in here. I have some problem when

  • 0

I hope I got the relevant code in here. I have some problem when I want to fetch the menu option that I’ve added into to menu_1. I have this function on_select(int) that I use to fetch one sub-menu’s options, which I do by using the display() function. But when I compile it will say that there are no function named display() in menu_option() class, which is the Base class, but what I want to is to access the display() function which is located in the sub_menu() class.

I have tried multiple thing to get the relevant object from the array without any success, so I’m here now asking for help with this one.

I have this following main()

#include <iostream>

using namespace std;

    #include "menu.h"

    int main()
    {


        sub_menu* main_menu = new sub_menu("Warehouse Store Menu");

        sub_menu* menu_1 = new sub_menu("Menu1");

        main_menu->add_option(new sub_menu("Menu2"));
        main_menu->add_option(menu_1);

        product_menu->add_option(new add_item("sub_item1"));
        product_menu->add_option(new add_item("sub_item2"));
        product_menu->add_option(new add_item("sub_item3"));  

        main_menu->display();
        main_menu->on_select(1);

        delete main_menu;

        return 0;
    }

header file

#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

const int MAX_SIZE = 9;

class menu_option
{
public:
    menu_option(string const& n) : title(n) {};
    virtual ~menu_option();

    virtual void on_select(int) = 0;
    string get_title() { return title; }
protected:
    string title;
};


/* ------------------------------------- */
class sub_menu : public menu_option
{

public:
    sub_menu(string const& n)
        : menu_option(n) { no_item = 0; }

    ~sub_menu() { delete[] list; };

    void on_select(int);

    void add_option(menu_option*);
    void display();

private:
    menu_option* list[MAX_SIZE];   //container for options in the sub_menu
    int no_item;

};

implement file

void sub_menu::on_select(int i)
{
    cout << (list[i])->get_title() << endl;
    cout << (list[i])->display() << endl; //<------ Doesn't work
}

void sub_menu::add_option(menu_option* item)
{
    list[no_item] = item;
    no_item++;
}

void sub_menu::display()
{
    cout << ">> " << get_title() << " <<"<< endl;
    for( int i = 0; i < no_item; i++ )
    {
       cout << setw(2) << i << ": " << (list[i])->get_title() << endl;
    }
}
  • 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-18T05:29:44+00:00Added an answer on May 18, 2026 at 5:29 am

    You can do what you want to do, but it’s bad. You have to cast down to sub_menu when you call display() in on_select(). Of course it’s not going to work the way you have it, and the compiler is telling you exactly why.

    The other option, which is probably better (though without a clear understanding of the problem space may not be the best) would be to add display() as a virtual function to the menu_option class.

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

Sidebar

Related Questions

I hope there's a SharePoint expert here on SO who can help with this.
Update : I edited the code, but the problem persists... Hi everyone, this is
I used the code from the following question to insert a YouTube video into
Hope anyone can shed light on this so I can use pens with dash
I hope this question does not come off as broad as it may seem
I hope everyone will pardon the length, and narrative fashion, of this question. I
I hope this question is not considered too basic for this forum, but we'll
I hope this is programmer-related question. I'm in the hobby business of C# programming.
I hope not everyone is using Rational Purify. So what do you do when
My hope is to one-click a shortcut, and get a grid of cygwin shells

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.