I’ve got the following g++ error
Menu.hpp:66:41: error: no matching function for call to ‘Menu::Stack<Submenu*>::push(Submenu*)’
Menu.hpp:66:41: note: candidate is:
Menu.hpp:14:21: note: void Menu::Stack<T>::push(T&) [with T = Submenu*]
Menu.hpp:14:21: note: no known conversion for argument 1 from ‘Submenu*’ to ‘Submenu*&’
How can such a conversion be impossible? On what sort of occasions does compiler issue such errors?
As to what I was actually doing:
- I have a class Submenu
- I have a class Menu which inherits from Submenu
- Menu has additional field of type
Stack<Submenu*>which maps remembering open submenus - All methods of Menu like "open menu", "click menu item" and so on refer to the Submenu that is currently on top() of the stack. Within class Submenu, they operate on the object itself.
- Menu has a public method of closing the current submenu and going up – i.e. popping the submenu from the stack.
- Menu can pop until it reaches itself, see below what does it mean.
Now is the part that is most probably the issue, namely, the constructor of Menu:
Menu() { stack.push( (Submenu*)this ); }
It does so because when all menus get closed, methods relative to stack.top() should refer to the Menu itself, being a kind of Submenu too (since it inherits from it).
EDIT:
I’ve made my own class Stack instead of using std::stack (as I initially suggested) and, as pointed out in the answer, there lied the problem. Excuse me for that inaccuracy.
Generally, you can only convert lvalues to references, not rvalues (a cast is an rvalue). You can convert rvalues to
constreferences, which is probably what you really want here — if you changeStack::pushto take aconst T &argument instead of aT &, the errors will go away.