No known conversion from pointer to pointer reference

I have the following g ++ error

Menu.hpp:66:41: error: no matching function for call toMenu::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 fromSubmenu*’ toSubmenu*&’

      

How is such a transformation impossible? When does the compiler throw such errors?

What I actually did:

  • I have a class submenu
  • I have a class menu that inherits from a submenu
  • The menu has an additional type field Stack<Submenu*>

    that displays the memorization of open submenus
  • All menu methods, such as "open menu", "click of a menu item", etc., refer to the submenu at the top () of the stack. Within a class submenu, they work on the object itself.
  • The menu has a public method to close the current submenu and go up - i.e. by choosing a submenu from the stack.
  • The menu can appear until it reaches itself, see below what this means.

Now this is the part that is most likely the problem, namely the Menu constructor:

Menu() { stack.push( (Submenu*)this ); }

      

This is because when all menus are closed, the methods related to stack.top () must be relative to the menu itself, being a kind of submenu (since it inherits from it).

EDIT:

I made my own Stack class instead of using std :: stack (as I originally suggested) and as stated in the answer the issue was resolved. Excuse me for this inaccuracy.

+3


source to share


2 answers


Generally, you can only convert lvalues ​​to references, not rvalues ​​(cast is rvalue). You can convert rvalues ​​to const

references which you probably really want here - if you change Stack::push

to take an argument const T &

instead T &

, the errors go away.



+5


source


A Submenu*&

may be assigned any Submenu*

. Does it make sense to do this with (Submenu*)this

? Not.

So C ++ stops you.



Now why is yours stack::push

accepting a link? Dunno.

0


source







All Articles