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

J

.J.T.

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?
 
J

John Harrison

..J.T. said:
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
 
I

int2str

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

typedef map<int,string> display_map; ....
and the error goes:

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

Cheers,
Andre
 
J

.J.T.

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(unsigned int)':
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkonce.t._S_oom_malloc(unsigned
int)+0x1a): undefined reference to `endl(ostream &)'
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkonce.t._S_oom_malloc(unsigned
int)+0x27): undefined reference to `cerr'
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkonce.t._S_oom_malloc(unsigned
int)+0x2c): undefined reference to `ostream::eek:perator<<(char const *)'
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkonce.t._S_oom_malloc(unsigned
int)+0x37): undefined reference to `ostream::eek:perator<<(ostream
&(*)(ostream &))'
/tmp/ccgd0GTp.o: In function `basic_string<char,
::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,
::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,
::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,
::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
 
C

Christian Meier

.J.T. said:
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]

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
 
J

John Harrison

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
 
J

John Harrison

..J.T. said:
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(unsigned int)':
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkonce.t._S_oom_malloc(unsigned
int)+0x1a): undefined reference to `endl(ostream &)'
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkonce.t._S_oom_malloc(unsigned
int)+0x27): undefined reference to `cerr'
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkonce.t._S_oom_malloc(unsigned
int)+0x2c): undefined reference to `ostream::eek:perator<<(char const *)'
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkonce.t._S_oom_malloc(unsigned
int)+0x37): undefined reference to `ostream::eek:perator<<(ostream
&(*)(ostream &))'
/tmp/ccgd0GTp.o: In function `basic_string<char,
::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,
::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,
::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,
::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

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

john
 
J

.J.T.

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..
 
J

.J.T.

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:)
 
R

Rolf Magnus

John said:
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.
 
O

Old Wolf

..J.T. said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top