c++ - "No known conversion" from const when passing "this" as a parameter -
i making game utilizes state stack manager keeps track of different game states, main menu.
however, have encountered problem can't seem solve.
this state class, stripped down contain offending code:
class statemanager; namespace first { namespace second { class state { friend class statemanager; protected: /** * associates state specified state manager instance. * @param statemanager instance associate state */ void associatewithmanager(statemanager *statemanager) { mstatemanager = statemanager; } private: statemanager *mstatemanager; }; } // second } // first
the following state manager, stripped down:
namespace first { namespace second { class statemanager { public: /** * adds state stack. * @param state state add */ state &add(state &state) { state.associatewithmanager(this); return state; } }; } // second } // first
when try compile this, following error (line numbers bit off, since have include guards, etc.):
src/statemanager.cc: in member function 'state& statemanager::add(state&)': src/statemanager.cc:7:34: error: no matching function call 'state::associatewithmanager(statemanager* const)' src/statemanager.cc:7:34: note: candidate is: in file included ./include/statemanager.h:4:0, src/statemanager.cc:1: ./include/state.h:29:10: note: void state::associatewithmanager(statemanager*) ./include/state.h:29:10: note: no known conversion argument 1 'statemanager* const' 'statemanager*'
apparently, this
pointer treated const pointer, though don't use const
keyword on add
method. i'm not sure going on here. this
pointer const
? i'm pretty sure i've used way in past no problems though.
also, way i'm going 'correct' way? or there better solution when comes letting state know manager? perhaps using singleton, i'm not big fan of that.
edit: realize forward declaration outside namespaces reason this. should accept mike's answer, since helped me come conclusion? or should post own?
is error? when compile it, error first:
test.cpp:8:31: error: ‘statemanager’ has not been declared
when declaring class friend, class must have been declared, otherwise declaration ignored. need declare class statemanager;
in surrounding namespace before definition of class state
. change, code compiles me.
Comments
Post a Comment