![]() |
|
|
Welcome to the { mindfrost82.com } forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Template syntax help
Compiles w/ MSVC (2008), fails with G++ (3.4.4 under cygwin), error
message included. Not sure what I'm doing wrong, I'm assuming the MSVC is being too permissive as usual... #include <map> #include <memory> using namespace std; template <class EventT, class StateId> class State; template <class EventT, class StateId> class StateMachine { public: typedef State<EventT, StateId> StateType; typedef std::map<StateId, StateType*> StateMap; void RemoveState(const StateId &stateId); }; template <class EventT, class StateId> class State {}; template <class EventT, class StateId> void StateMachine<EventT, StateId>::RemoveState( const StateId &stateId) { typedef StateMap::iterator StateMapIt; //Errors start here StateMapIt it; } int main(int argc, char *argv[]) { StateMachine<int, int> x; x.RemoveState(0); return 0; } .../src/TestWorkspace/TestWorkspace.cpp: In member function `void StateMachine<Ev entT, StateId>::RemoveState(const StateId&)': .../src/TestWorkspace/TestWorkspace.cpp:24: error: expected init- declarator befor e "StateMapIt" .../src/TestWorkspace/TestWorkspace.cpp:24: error: expected `,' or `;' before "St ateMapIt" .../src/TestWorkspace/TestWorkspace.cpp:25: error: `StateMapIt' undeclared (first use this function) .../src/TestWorkspace/TestWorkspace.cpp:25: error: (Each undeclared identifier is reported only once for each function it appears in.) .../src/TestWorkspace/TestWorkspace.cpp:25: error: expected `;' before "it" make: *** [../obj/cygwin/debug/TestWorkspace/TestWorkspace.o] Error 1 -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: Template syntax help
cat314159@yahoo.com wrote:
> Compiles w/ MSVC (2008), fails with G++ (3.4.4 under cygwin), error > message included. Not sure what I'm doing wrong, I'm assuming the > MSVC is being too permissive as usual... > > #include <map> > #include <memory> > using namespace std; > > template <class EventT, class StateId> class State; > > template <class EventT, class StateId> class StateMachine > { > public: > > typedef State<EventT, StateId> StateType; > typedef std::map<StateId, StateType*> StateMap; > > void RemoveState(const StateId &stateId); > }; > > template <class EventT, class StateId> > class State {}; > > template <class EventT, class StateId> > void StateMachine<EventT, StateId>::RemoveState( > const StateId &stateId) > { > typedef StateMap::iterator StateMapIt; //Errors start here typedef typename StateMap::iterator StateMapIt; > StateMapIt it; > } > StateMap::iterator is a dependent name, so the compiler needs you to help it by explicitly telling it that iterator is a type. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: Template syntax help
On 2008-08-24 01:58, cat314159@yahoo.com wrote:
> Compiles w/ MSVC (2008), fails with G++ (3.4.4 under cygwin), error > message included. Not sure what I'm doing wrong, I'm assuming the > MSVC is being too permissive as usual... > > #include <map> > #include <memory> > using namespace std; > > template <class EventT, class StateId> class State; > > template <class EventT, class StateId> class StateMachine > { > public: > > typedef State<EventT, StateId> StateType; > typedef std::map<StateId, StateType*> StateMap; > > void RemoveState(const StateId &stateId); > }; > > template <class EventT, class StateId> > class State {}; > > template <class EventT, class StateId> > void StateMachine<EventT, StateId>::RemoveState( > const StateId &stateId) > { > typedef StateMap::iterator StateMapIt; //Errors start here Make that "typedef typename StateMap::iterator StateMapIt;" -- Erik Wikström [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: Template syntax help
cat314159@yahoo.com wrote:
> Compiles w/ MSVC (2008), fails with G++ (3.4.4 under cygwin), error > message included. Not sure what I'm doing wrong, I'm assuming the > MSVC is being too permissive as usual... > template <class EventT, class StateId> > void StateMachine<EventT, StateId>::RemoveState( > const StateId &stateId) > { > typedef StateMap::iterator StateMapIt; //Errors start here Try: typedef typename StateMap::iterator StateMapIt; Andre' -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: Template syntax help
cat314159@yahoo.com schrieb: > Compiles w/ MSVC (2008), fails with G++ (3.4.4 under cygwin), error > message included. Not sure what I'm doing wrong, I'm assuming the > MSVC is being too permissive as usual... > [snip] > template <class EventT, class StateId> > void StateMachine<EventT, StateId>::RemoveState( > const StateId &stateId) > { > typedef StateMap::iterator StateMapIt; //Errors start here > StateMapIt it; > } You make a type that is dependent on a template and you have to use the 'typename' keyword: typedef typename StateMap::iterator StateMapIt; // will solve your error I guess -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: Template syntax help
If the name used depends on a template type as the case shown below
you need to prefix the declaration with the keyword 'typename'. In this case the name 'something' depends on the type T. template <typename T> class A { public: typename T::something x; }; class B { public: typedef int something; }; A<B> a; There's a full explanation here http://www.gotw.ca/gotw/035.htm. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
![]() |
|
| Thread Tools | Search this Thread |
| Display Modes | |
|
|