473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
10 3173
..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

..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
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
".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
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
..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
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
>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
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
..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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
3
by: Gizmo | last post by:
hello all have been trying to write a Mid() function a bit like the one in vb. i come to compile it and there are no errors however when i run it an error accours and it says the program has to...
8
by: Mr. x | last post by:
Hello, I wrote a javascript, and when I open the html by the browser, I get the message : Error in page. It is about tousand lines, and I cannot figure out in a simple way, where is the...
17
by: Steve Jorgensen | last post by:
If you've ever employed custom error numbers and messages in you programs, you've probably ended up with code similar to what I've ended up with in the past something like... <code> public...
21
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the variable is defined as private at form-level. So...
13
by: a.zeevi | last post by:
free() multiple allocation error in C ==================================== Hi! I have written a program in C on PC with Windows 2000 in a Visual C environment. I have an error in freeing...
3
by: Don | last post by:
I have an ASP.NET 2.0 application (in VB) based on the Personal Site Starter kit that runs perfectly (no erros, no warnings, no exceptions) when run from Visual Studio 2005 Team Suite using the...
8
by: Taras_96 | last post by:
Hi everyone, We' ve come to the conclusion that we wish the user to be directed to an error page if javascript is disabled <enter comment about how a webpage shouldn't rely on javascript here :)...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
10
by: happyse27 | last post by:
Hi All, I got this apache errors(see section A1 and A2 below) when I used a html(see section b below) to activate acctman.pl(see section c below). Section D below is part of the configuration...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.