I have the following problem:
I am trying to make some sort of base class menu that i can then use to derive other menu's from. Those menu's should then be able to interact with each other.
And, i have most of the idea figured out and I thought out how i want to do it. But when i started coding i found a slight... difficulty.
It might be easy to overcome, but google let me down :( and my own imagination made one happy jump, but well, that didn't do the trick either.
So, the problem is this:
My class API_MENU_BASIC (yes, it really is a bad name, inspiration lacks me when it comes to names ) has following vectors:
Expand|Select|Wrap|Line Numbers
- /* Menu choices */
- /** A vector containing all possible MENU's and their descriptions. */
- static vector<pair<API_MENU_BASIC*, string> > fMenus;
- static bool fMenus_set; /**< fMenu's has already been set. */
- /** Vector containing all the valid choices for the menu class.
- * It might be that a class can not acces all of the menu's in fMenus.
- * A class will thus, always acces them through the fChoices vector. */
- vector<int> fChoices;
- bool fChoices_set; /**< fChoices has been set. */
This vector may also be initialized only once, that's where the static bool fMenus_set is for.
The second vector keeps track of what are valid menu choices for each derived class. Because it is very well possible that a derived class can only access a 'print' menu and none of the 'modify' menu's.
(This vector may also be initialized only once, again, fChoices_set )
Both vectors have an 'initialization' function.
Expand|Select|Wrap|Line Numbers
- /* Build menu's */
- static void setMenus( const vector<pair<API_MENU_BASIC*, string> >& );
- void setChoices( const vector<int>& );
Now, where the problem really is:
The menu navigators:
Expand|Select|Wrap|Line Numbers
- /* Menu navigators */
- void expand( const int id );
- virtual void open() = 0;
It gets the object with given index from the fMenus (static :S) vector and 'loads' it onto the
Expand|Select|Wrap|Line Numbers
- /**
- * Menu pointer: When we expand a new menu, the menu will first be loaded
- * onto the fActive field and then opened.
- */
- API_MENU_BASIC* fActive;
This is the implementation for the expand function:
Expand|Select|Wrap|Line Numbers
- /** Run the menu that is associated with index 'id' in the
- * vector of valid choices.
- * @param id Selected choice
- * @return void
- * @exception none
- */
- void API_MENU_BASIC::expand( const int id ){
- fActive = fMenus[fChoices[id]].first;
- fActive->open();
- }
Expand|Select|Wrap|Line Numbers
- /* Menu inspectors */
- static API_MENU_BASIC* getMenu( const int& id );
- static string& getDescription( const int& id );
Now the compiler doesn't like me for what i'm trying, it's just, i don't know what it is that i'm doing wrong. And, I don't dare say this but, i didn't even find any usefull information in Strousstrup :(. Which is most probably my bad, but, alas. I didn't find it.
The compiler errors i'm getting are:
- When i try to make getMenu, getDescription and setMenus static:
API_MENU_BASIC.cpp:49: error: cannot declare member function ‘static void MENU::API_MENU_BASIC::setMenus(const std::vector<std::pair<MENU::API_MENU_BASIC*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<MENU::API_MENU_BASIC*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&)’ to have static linkage
API_MENU_BASIC.cpp:78: error: cannot declare member function ‘static MENU::API_MENU_BASIC* MENU::API_MENU_BASIC::getMenu(const int&)’ to have static linkage
API_MENU_BASIC.cpp:88: error: cannot declare member function ‘static std::string& MENU::API_MENU_BASIC::getDescription(const int&)’ to have static linkage
And when i don't make them static he sais that fMenus isn't in scope.
Any help with this would be enormously appreciated, i have no idea how to fix this.
Thanks in advance
-Tom