By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,435 Members | 1,485 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,435 IT Pros & Developers. It's quick & easy.

STL map error (I don't get it..)

P: n/a
Hello all,
I have a strange error when try to use map<int, string> (using gcc
version 2.95.2).

I have this code in header file (display_map.h):

#ifndef DISPLAY_MAP_H
#define DISPLAY_MAP_H

#include <map>
#include <string>

typedef map<int,string> display_map;
display_map DM;
DM[0]=" ";
DM[1]="o";
DM[2]=".";

#endif

The main program is that simple:

#include "display_map.h"

int main(){
return 0;
}

and the error goes:
---------------------------
display_map.h:9: ANSI C++ forbids declaration `DM' with no type
display_map.h:9: conflicting types for `int DM[0]'
display_map.h:8: previous declaration as `class display_map DM'
display_map.h:9: invalid initializer
....
(and the same about DM[1], DM[2])
...
cc1plus: register name not specified for ` /* decl error */ '
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../../../include/g++-3/stl_map.h:76:
Internal compiler error in `make_decl_rtl', at varasm.c:738
Please submit a full bug report.
-----------------------------

Why "declaration `DM' with no type"?
What "conflicting types"?
Where "previous declaration"?

Is this some kind of gcc bug, or is there something I don't know about
map usege?

Oct 26 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
..J.T. wrote:
Hello all,
I have a strange error when try to use map<int, string> (using gcc
version 2.95.2).

I have this code in header file (display_map.h):

#ifndef DISPLAY_MAP_H
#define DISPLAY_MAP_H

#include <map>
#include <string>

typedef map<int,string> display_map;
display_map DM;
DM[0]=" ";
DM[1]="o";
DM[2]=".";

#endif

The main program is that simple:

#include "display_map.h"

int main(){
return 0;
}

and the error goes:
---------------------------
display_map.h:9: ANSI C++ forbids declaration `DM' with no type
display_map.h:9: conflicting types for `int DM[0]'
display_map.h:8: previous declaration as `class display_map DM'
display_map.h:9: invalid initializer
...
(and the same about DM[1], DM[2])
..
cc1plus: register name not specified for ` /* decl error */ '
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../../../include/g++-3/stl_map.h:76:
Internal compiler error in `make_decl_rtl', at varasm.c:738
Please submit a full bug report.
-----------------------------

Why "declaration `DM' with no type"?
What "conflicting types"?
Where "previous declaration"?

Is this some kind of gcc bug, or is there something I don't know about
map usege?


Nothing to do with maps, your code is illegal because you cannot put
statements outside of a function.

Try this

// header file

typedef map<int,string> display_map;
display_map DM;

// main program

int main()
{
DM[0]=" ";
DM[1]="o";
DM[2]=".";
}

john
Oct 26 '05 #2

P: n/a

..J.T. wrote:
#include <map>
#include <string>

typedef map<int,string> display_map; .... and the error goes:
---------------------------
display_map.h:9: ANSI C++ forbids declaration `DM' with no type


map and string are undefined here.
You need to specify (std::map / std::string), or put the appropriate
"using" clauses in place.

Cheers,
Andre

Oct 26 '05 #3

P: n/a
Thanks for reply.

Using John's and Andre's tips code looks now like this:
display_map.h
---------------------------------------------------
#ifndef DISPLAY_MAP_H
#define DISPLAY_MAP_H
#include <map>
#include <string>

typedef std::map<int,std::string> display_map;
display_map DM;
#endif

main.cpp
----------------------------------------------
#include "display_map.h"
#include <string>
int main(){
DM[0]=string(" ");
DM[1]=string("o");
DM[2]=string(".");
return 0;
}
End errors goes:
---------------------------------------------
/tmp/ccgd0GTp.o: In function
`__malloc_alloc_template<0>::_S_oom_malloc(unsigne d int)':
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkon ce.t._S_oom_malloc(unsigned
int)+0x1a): undefined reference to `endl(ostream &)'
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkon ce.t._S_oom_malloc(unsigned
int)+0x27): undefined reference to `cerr'
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkon ce.t._S_oom_malloc(unsigned
int)+0x2c): undefined reference to `ostream::operator<<(char const *)'
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkon ce.t._S_oom_malloc(unsigned
int)+0x37): undefined reference to `ostream::operator<<(ostream
&(*)(ostream &))'
/tmp/ccgd0GTp.o: In function `basic_string<char,
string_char_traits<char>, __default_alloc_template<true, 0>
::Rep::copy(unsigned int, char const *, unsigned int)': /tmp/ccgd0GTp.o(.basic_string<char, string_char_traits<char>,
__default_alloc_template<true, 0> >::Rep::gnu.linkonce.t.copy(unsigned
int, char const *, unsigned int)+0x33): undefined reference to
`string_char_traits<char>::copy(char *, char const *, unsigned int)'
/tmp/ccgd0GTp.o: In function `basic_string<char,
string_char_traits<char>, __default_alloc_template<true, 0>::Rep::move(unsigned int, char const *, unsigned int)': /tmp/ccgd0GTp.o(.basic_string<char, string_char_traits<char>,
__default_alloc_template<true, 0> >::Rep::gnu.linkonce.t.move(unsigned
int, char const *, unsigned int)+0x33): undefined reference to
`string_char_traits<char>::move(char *, char const *, unsigned int)'
/tmp/ccgd0GTp.o: In function `basic_string<char,
string_char_traits<char>, __default_alloc_template<true, 0>::replace(unsigned int, unsigned int, char const *, unsigned int)': /tmp/ccgd0GTp.o(.basic_string<char, string_char_traits<char>,
__default_alloc_template<true, 0> >::gnu.linkonce.t.replace(unsigned
int, unsigned int, char const *, unsigned int)+0x31): undefined
reference to `__out_of_range(char const *)'
/tmp/ccgd0GTp.o(.basic_string<char, string_char_traits<char>,
__default_alloc_template<true, 0> >::gnu.linkonce.t.replace(unsigned
int, unsigned int, char const *, unsigned int)+0x8e): undefined
reference to `__length_error(char const *)'
/tmp/ccgd0GTp.o: In function `basic_string<char,
string_char_traits<char>, __default_alloc_template<true, 0>::assign(char const *)':

/tmp/ccgd0GTp.o(.basic_string<char, string_char_traits<char>,
__default_alloc_template<true, 0> >::gnu.linkonce.t.assign(char const
*)+0x16): undefined reference to `string_char_traits<char>::length(char
const *)'
collect2: ld returned 1 exit status
-----------------------------------------------------------
Can anyone explain my what's going on here?

Somehow I feel this is not my day... or I need more coffe.. or both..

Oct 26 '05 #4

P: n/a
".J.T." <j.**********@gmail.com> schrieb im Newsbeitrag
news:11**********************@f14g2000cwb.googlegr oups.com...
Thanks for reply.

Using John's and Andre's tips code looks now like this:
display_map.h
---------------------------------------------------
#ifndef DISPLAY_MAP_H
#define DISPLAY_MAP_H
#include <map>
#include <string>

typedef std::map<int,std::string> display_map;
display_map DM;
#endif

main.cpp
----------------------------------------------
#include "display_map.h"
#include <string>
int main(){
DM[0]=string(" ");
DM[1]=string("o");
DM[2]=string(".");
return 0;
}
End errors goes:
--------------------------------------------- [error messages snipped] -----------------------------------------------------------
Can anyone explain my what's going on here?

Somehow I feel this is not my day... or I need more coffe.. or both..


In your main() function you use string without std::
Use either "using std::string" or replace your main function with the
following:

int main(){
DM[0]=std::string(" ");
DM[1]=std::string("o");
DM[2]=std::string(".");
}

By the way: returning 0 from main is not necessary.

Greetings Chris
Oct 26 '05 #5

P: n/a
in*****@gmail.com wrote:
.J.T. wrote:
#include <map>
#include <string>

typedef map<int,string> display_map;


...
and the error goes:
---------------------------
display_map.h:9: ANSI C++ forbids declaration `DM' with no type

map and string are undefined here.
You need to specify (std::map / std::string), or put the appropriate
"using" clauses in place.

Cheers,
Andre


The gcc 2.95 compiler doesn't have a std namespace.

john
Oct 26 '05 #6

P: n/a
..J.T. wrote:
Thanks for reply.

Using John's and Andre's tips code looks now like this:
display_map.h
---------------------------------------------------
#ifndef DISPLAY_MAP_H
#define DISPLAY_MAP_H
#include <map>
#include <string>

typedef std::map<int,std::string> display_map;
display_map DM;
#endif

main.cpp
----------------------------------------------
#include "display_map.h"
#include <string>
int main(){
DM[0]=string(" ");
DM[1]=string("o");
DM[2]=string(".");
return 0;
}
End errors goes:
---------------------------------------------
/tmp/ccgd0GTp.o: In function
`__malloc_alloc_template<0>::_S_oom_malloc(unsigne d int)':
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkon ce.t._S_oom_malloc(unsigned
int)+0x1a): undefined reference to `endl(ostream &)'
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkon ce.t._S_oom_malloc(unsigned
int)+0x27): undefined reference to `cerr'
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkon ce.t._S_oom_malloc(unsigned
int)+0x2c): undefined reference to `ostream::operator<<(char const *)'
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkon ce.t._S_oom_malloc(unsigned
int)+0x37): undefined reference to `ostream::operator<<(ostream
&(*)(ostream &))'
/tmp/ccgd0GTp.o: In function `basic_string<char,
string_char_traits<char>, __default_alloc_template<true, 0>
::Rep::copy(unsigned int, char const *, unsigned int)':


/tmp/ccgd0GTp.o(.basic_string<char, string_char_traits<char>,
__default_alloc_template<true, 0> >::Rep::gnu.linkonce.t.copy(unsigned
int, char const *, unsigned int)+0x33): undefined reference to
`string_char_traits<char>::copy(char *, char const *, unsigned int)'
/tmp/ccgd0GTp.o: In function `basic_string<char,
string_char_traits<char>, __default_alloc_template<true, 0>
::Rep::move(unsigned int, char const *, unsigned int)':


/tmp/ccgd0GTp.o(.basic_string<char, string_char_traits<char>,
__default_alloc_template<true, 0> >::Rep::gnu.linkonce.t.move(unsigned
int, char const *, unsigned int)+0x33): undefined reference to
`string_char_traits<char>::move(char *, char const *, unsigned int)'
/tmp/ccgd0GTp.o: In function `basic_string<char,
string_char_traits<char>, __default_alloc_template<true, 0>
::replace(unsigned int, unsigned int, char const *, unsigned int)':


/tmp/ccgd0GTp.o(.basic_string<char, string_char_traits<char>,
__default_alloc_template<true, 0> >::gnu.linkonce.t.replace(unsigned
int, unsigned int, char const *, unsigned int)+0x31): undefined
reference to `__out_of_range(char const *)'
/tmp/ccgd0GTp.o(.basic_string<char, string_char_traits<char>,
__default_alloc_template<true, 0> >::gnu.linkonce.t.replace(unsigned
int, unsigned int, char const *, unsigned int)+0x8e): undefined
reference to `__length_error(char const *)'
/tmp/ccgd0GTp.o: In function `basic_string<char,
string_char_traits<char>, __default_alloc_template<true, 0>
::assign(char const *)':


/tmp/ccgd0GTp.o(.basic_string<char, string_char_traits<char>,
__default_alloc_template<true, 0> >::gnu.linkonce.t.assign(char const
*)+0x16): undefined reference to `string_char_traits<char>::length(char
const *)'
collect2: ld returned 1 exit status
-----------------------------------------------------------
Can anyone explain my what's going on here?

Somehow I feel this is not my day... or I need more coffe.. or both..


The problem may be that you are using gcc to compile instead of g++.

john
Oct 26 '05 #7

P: n/a
I've tryed what you sugested
Use either "using std::string" or replace your main function with the


but I still get the same errors..

There _must_ be something I'm missing in here..

Oct 26 '05 #8

P: n/a
>The problem may be that you are using gcc to compile instead of g++.

_Thank You, John Harrison_ :)

The thing was I'm using g++ in my Makefile, but I just wanted to test
something and compiled small part of code from command line (with empty
main file) and (why? oh why?) I _did_ typed gcc instead g++.

Thanks again for clearing my mind:)

Oct 26 '05 #9

P: n/a
John Harrison wrote:
#include <map>
#include <string>

typedef map<int,string> display_map;


...
and the error goes:
---------------------------
display_map.h:9: ANSI C++ forbids declaration `DM' with no type

map and string are undefined here.
You need to specify (std::map / std::string), or put the appropriate
"using" clauses in place.

Cheers,
Andre


The gcc 2.95 compiler doesn't have a std namespace.


It does. In fact, in gcc 2.95, namespace std is just an alias for the global
namespace. Of course that's not standard compliant, but it means that using
the std:: prefix for map and string is ok with that compiler. On less
ancient versions, it's even mandatory, since those versions implement it
correctly.

Oct 26 '05 #10

P: n/a
..J.T. wrote:
Thanks for reply.

Using John's and Andre's tips code looks now like this:
display_map.h
---------------------------------------------------
#ifndef DISPLAY_MAP_H
#define DISPLAY_MAP_H
#include <map>
#include <string>

typedef std::map<int,std::string> display_map;
display_map DM;
#endif
Please note that declaring DM in the header is generally a bad
idea. If you have another .cpp file that includes the same
header, then you cause undefined behaviour. (Probably what will
happen is that each .cpp file will have its own map, and they
will all be called DM).

If you want one map that is visible from many .cpp files, then
the header must have a line:

extern display_map DM;

and exactly one of the .cpp files must have:

display_map DM;
#include "display_map.h"
#include <string>
int main(){
DM[0]=string(" ");
DM[1]=string("o");
DM[2]=string(".");
You can just write:

DM[0] = "";

etc., because std::string can be initialized from a string literal.
Somehow I feel this is not my day... or I need more coffe.. or both..


Consider upgrading to GCC 4.
2.95 has many bugs and is not very standards-compliant so you
may run into other hard-to-diagnose errors.

Oct 26 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.