[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

map of map.

aTuL

12/1/2008 11:26:00 PM

Hi All,

I want to create a map of key and another map object (it is another
map of key value pairs). I have declared it as
map<int, map<short, short>> mpObj; it gives me an error that, "error:
ISO C++ forbids declaration of ‘map’ with no type". Does this mean I
can not create a map of a map the object? Have I done something wrong?
Any pointers to any tutorial?

Atul.
6 Answers

Christian Hackl

12/1/2008 11:44:00 PM

0

aTuL ha scritto:

> I want to create a map of key and another map object (it is another
> map of key value pairs). I have declared it as
> map<int, map<short, short>> mpObj;

You have to write it like this:

map<int, map<short, short> > mpObj;


Note that the next version of C++ will fix this problem.


--
Christian Hackl

Christian Hackl

12/1/2008 11:54:00 PM

0

aTuL ha scritto:

> I want to create a map of key and another map object (it is another
> map of key value pairs). I have declared it as
> map<int, map<short, short>> mpObj; it gives me an error that, "error:
> ISO C++ forbids declaration of ?map? with no type". Does this mean I
> can not create a map of a map the object? Have I done something wrong?
> Any pointers to any tutorial?

On second thought, the >> is probably not the cause of this particular
error. Have you included <map> and is there a using std::map somewhere?

Here's a minimal example that compiles fine:

#include <map>
int main()
{
std::map<int, std::map<short, short> > mpObj;
}


--
Christian Hackl

Senthil

12/1/2008 11:55:00 PM

0

<snip>
>
> map<int, map<short, short> > mpObj;
>

Hard to note if you are new, you need the the space between the two
'>'.

<snip>

jason.cipriani@gmail.com

12/2/2008 3:54:00 AM

0

On Dec 1, 6:54 pm, Christian Hackl <ha...@sbox.tugraz.at> wrote:
> aTuL ha scritto:
>
> > I want to create a map of key and another map object (it is another
> > map of key value pairs). I have declared it as
> > map<int, map<short, short>> mpObj; it gives me an error that, "error:
> > ISO C++ forbids declaration of ‘map’ with no type". Does this mean I
> > can not create a map of a map the object? Have I done something wrong?
> > Any pointers to any tutorial?
>
> On second thought, the >> is probably not the cause of this particular
> error. Have you included <map> and is there a using std::map somewhere?

The error he got is reasonable:

map<int, map<short, short>> mpObj;

Could be parsed as:

(map < int) , (map < short) , (short >> mpObj);

And the error "declaration of map with no type" comes from the
seemingly implicit declaration of "map" in "(map < int)".

Jason

Bill Davy

12/2/2008 8:01:00 AM

0

If it's hard to write it will be hard to read (especially in a year's time).

So why not divide and conquer?

typedef map<short,short> MapPhoneToMobileT;
typedef map<int, MapPhoneToMobileT> MapCustomerIdToMobileT;

Bill

"aTuL" <atulskulkarni@gmail.com> wrote in message
news:0f7dab35-5cb1-4cae-be1e-c864043c5ab6@f3g2000yqf.googlegroups.com...
Hi All,

I want to create a map of key and another map object (it is another
map of key value pairs). I have declared it as
map<int, map<short, short>> mpObj; it gives me an error that, "error:
ISO C++ forbids declaration of ?map? with no type". Does this mean I
can not create a map of a map the object? Have I done something wrong?
Any pointers to any tutorial?

Atul.


James Kanze

12/2/2008 1:27:00 PM

0

On Dec 2, 4:54 am, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.com> wrote:
> On Dec 1, 6:54 pm, Christian Hackl <ha...@sbox.tugraz.at> wrote:

> > aTuL ha scritto:

> > > I want to create a map of key and another map object (it
> > > is another map of key value pairs). I have declared it as
> > > map<int, map<short, short>> mpObj; it gives me an error
> > > that, "error: ISO C++ forbids declaration of ?map? with no
> > > type". Does this mean I can not create a map of a map the
> > > object? Have I done something wrong? Any pointers to any
> > > tutorial?

> > On second thought, the >> is probably not the cause of this
> > particular error. Have you included <map> and is there a
> > using std::map somewhere?

> The error he got is reasonable:

It's hard to say what is reasonable when it comes to compiler
error messages these days:-).

> map<int, map<short, short>> mpObj;

> Could be parsed as:

> (map < int) , (map < short) , (short >> mpObj);

> And the error "declaration of map with no type" comes from the
> seemingly implicit declaration of "map" in "(map < int)".

Trying to indicate grouping with parentheses in this case
doesn't work. What the compiler sees (and is required to see,
according to the standard) is:
symbol map
punct '<', open template arg. list, if map is found at
global scope and is a template, otherwise less than
keyword int
punct ','
symbol map
punct '<' (as above)
short keyword
keyword short
punct ','
keyword short
punct '>>', which can only be shift right, regardless of
context
symbol myObj
punct ';'
Obviously, that token sequence can't be legal, regardless of
whether the compiler finds the symbol map or not. (If it
doesn't find map, there's no way < can have a keyword typename
as its right argument, and if it does, the opening of the
template arguments must be closed before the ';'.)

The next version of the standard will contain special wording or
grammar productions to the effect that you can close two
template argument lists at once (provided two are open) with a
>> token.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34