STL地图错误(我不明白..) [英] STL map error (I don't get it..)
问题描述
大家好,
尝试使用map< int,string>时出现奇怪错误(使用gcc
版本2.95.2)。
我在头文件(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
主要程序很简单:
#include" display_map.h"
>
int main(){
返回0;
}
,错误如下:
---------------------------
display_map.h:9:ANSI C ++禁止声明没有类型的'DM''
display_map.h:9:'int DM [0]'的冲突类型
display_map.h:8:之前的声明为` class display_map DM''
display_map.h:9:初始化程序无效
....
(和DM一样[1] ,DM [2])
...
cc1plus:寄存器名称未指定为`/ * decl错误* /''
/ usr / local / lib / gcc-lib / i686-pc-linux-gnu / 2.95.2 /../../../../ include / g ++ - 3 / stl_map.h:76:
在`make_decl_rtl''中的内部编译器错误,在varasm.c:738
请提交完整的错误报告。
-----------------------------
为什么要声明`DM''没有类型?
什么冲突类型"?
哪里有上一个声明?
这是某种gcc bug,还是有什么东西我不知道
map usege?
.. JT写道:大家好,
尝试使用map< int,string>时出现奇怪的错误(使用gcc
版本2.95.2)。
我在头文件(display_map.h)中有这个代码:
#ifndef DISPLAY_MAP_H
#定义DISPLAY_MAP_H
#include< map>
#include< string>
typedef map< int,string> display_map;
display_map DM;
DM [0] =" " ;;
DM [1] =" o";
DM [2] ="。" ;;
#endif
主要程序很简单:
#include" display_map.h"
int main(){
返回0;
}
---------------------------
display_map.h: 9:ANSI C ++禁止声明`DM''没有类型
display_map.h:9:`int DM [0]'的冲突类型
display_map.h:8:上一个声明为`class display_map DM''
display_map.h:9:无效的初始化程序
...
(和DM [1],DM [2]相同)
..
> cc1plus:没有为`/ * decl错误指定注册名称* /''
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../ ../../include/g++-3/stl_map.h:76:
`make_decl_rtl''中的内部编译器错误,在varasm.c:738
请提交完整的错误报告。 /> -----------------------------
为什么声明`DM''没有类型" ;?
什么是confl icting类型"?
哪里有以前的声明?
这是某种gcc bug,还是有什么东西我不知道
map usege?
与地图无关,你的代码是非法的,因为你不能把
语句放在一个函数之外。
>
试试这个
//头文件
typedef map< int,string> display_map;
display_map DM;
//主程序
int main()
{
DM [0] =" " ;;
DM [1] =" o";
DM [2] ="。" ;;
}
john
..JT写道:#include< map>
#include< string>
typedef map< int,string> display_map;
....并且错误如下:
---------------------------
display_map。 h:9:ANSI C ++禁止声明`DM''没有类型
map和string在这里是未定义的。
你需要指定(std :: map / std :: string),或者用适当的
"使用"条款到位。
干杯,
Andre
感谢您的回复。
使用John'和Andre'的提示代码现在看起来像这样:
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] =字符串(" o");
DM [2] =字符串("。");
返回0;
}
结束错误:
----------- ----------------------------------
/tmp/ccgd0GTp.o:In功能
`__malloc_alloc_template&℃,GT; :: _ S_oom_malloc(无符号
d INT) '':
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkon ce.t._S_oom_malloc(unsigned
int)+ 0x1a):未定义的引用`endl(ostream&)''
/tmp/ccgd0GTp.o(.__malloc_alloc_template< ; 0> :: gnu.linkon ce.t._S_oom_malloc(unsigned
int)+ 0x27):未定义引用`cerr''
/tmp/ccgd0GTp.o (.__ malloc_alloc_template℃, > :: gnu.linkon ce.t._S_oom_malloc(unsigned
int)+ 0x2c):对`ostream :: operator<<(char const *)''的未定义引用
/tmp/ccgd0GTp.o(.__malloc_alloc_template<0>::gnu.linkon ce.t._S_oom_malloc(unsigned
int)+ 0x37):对ostream :: operator<的未定义引用;<(ostream
&(*)(ostream&))''
/tmp/ccgd0GTp.o:在函数`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):未定义引用
`string_char_traits< ; char> :: copy(char *,char const *,unsigned int)''
/tmp/ccgd0GTp.o:在函数`basic_string< char,
string_char_traits< ;炭>中__default_alloc_template<真,0> ::代表::移动(无符号整数,字符常量*,无符号整型) '':
/tmp/ccgd0GTp.o(.basic_string<char,string_char_traits<炭> ,
__default_alloc_template< true,0>> :: Rep :: gnu.linkonce.t.move(unsigned
int,char const *,unsigned int)+ 0x33 ):未定义引用
`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 c为C har,string_char_traits< char>,
__default_alloc_template< true,0> > :: gnu.linkonce.t.replace(unsigned
int,unsigned int,char const *,unsigned int)+ 0x31):undefined
对`__out_of_range的引用(字符常量*) ''
/tmp/ccgd0GTp.o(.basic_string<char,string_char_traits<炭>中
__default_alloc_template<真,0>> :: gnu.linkonce.t.replace(unsigned
int,unsigned int,char const *,unsigned int)+ 0x8e):undefined
引用`__length_error(char const *) )''
/tmp/ccgd0GTp.o:在函数`basic_string< char,
string_char_traits< char> ;,__default_alloc_template< true,0> :: assign(char const *) '':
/tmp/ccgd0GTp.o(.basic_string<char,string_char_traits<炭>中
__default_alloc_template<真,0>> :: gnu.linkonce.t.assign(char const
*)+ 0x16):对`string_char_traits< char> :: length(char
const *)的未定义引用''
collect2:ld返回1退出状态
------------------------------------------ -----------------
谁能解释一下我在这里发生了什么?
莫名其妙我觉得这不是我的日子......或者我需要更多的咖啡......或者两者兼而有..
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.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
..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
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..
这篇关于STL地图错误(我不明白..)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!