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.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-24-2008, 12:58 AM
cat314159@yahoo.com
 
Posts: n/a
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! ]

Reply With Quote
  #2 (permalink)  
Old 08-25-2008, 03:51 AM
red floyd
 
Posts: n/a
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! ]

Reply With Quote
  #3 (permalink)  
Old 08-25-2008, 03:56 AM
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
 
Posts: n/a
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! ]

Reply With Quote
  #4 (permalink)  
Old 08-25-2008, 03:58 AM
Andre Poenitz
 
Posts: n/a
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! ]

Reply With Quote
  #5 (permalink)  
Old 08-25-2008, 04:01 AM
klaus triendl
 
Posts: n/a
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! ]

Reply With Quote
  #6 (permalink)  
Old 08-25-2008, 04:01 AM
sashang@gmail.com
 
Posts: n/a
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! ]

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 04:12 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Loans | Cheat Codes | Online Dating | Loans | Web Advertising



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114