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;
    }
}
2
  • 1
    Please cut your code down to the smallest set of lines that demonstrates your problem. Nobody wants to read 2 pages of dense text to discover if they want to solve your problem or not. Commented Nov 23, 2010 at 19:53
  • ah sorry, I didn't mean to post a huge set of code. It just that I don't know whether I should post the relevant code, or post the code which produce the error which in this case would be the on_select(int) function. Please do explain for me, so I don't do same error next time Commented Nov 23, 2010 at 20:07

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Could you tell me more or refer to some page why it's bad to cast?
1

To solve your immediate problem you'll want to use dynamic_cast to turn a menu_option* into a sub_menu*, like so:

sub_menu* submenu(dynamic_cast<sub_menu*>(list[i]));

Note that if the cast fails (i.e., the menu_option pointed to by list[i] is not a sub_menu after all) the value of the submenu pointer will be NULL, so make sure you check that it is a valid pointer before using it in subsequent code.

5 Comments

Do NOT use reinterpret_cast!! If you are to cast down, use a static_cast. If you want to verify that the cast is correct, use dynamic_cast. -1
@Noah: Ack! That's the one I meant, sorry. Corrected.
-1 redacted, but I'd still be happier with the answer if it mentioned that this almost certainly isn't a /good/ fix.
thanks you and Noah had the same solution, but I picked Noah's as solution, and gave you +1 for useful answer. Anyway thanks for the help
@Noah: That's why I prefixed the answer with "To solve your immediate problem...", but yes, being more explicit about a proper solution would have been better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.