C++ c+中运行时的分段错误+;班

C++ c+中运行时的分段错误+;班,c++,stl,C++,Stl,嗨,我在运行下面的代码时看到这个错误。“分割错误(核心转储)”我不能确定原因。\n有人帮帮我。谢谢 #ifndef __LIB_TAG_STRINGS_H__ #define __LIB_TAG_STRINGS_H__ //============ //stl #include <deps.h> #include <string> namespace tags { const std::string tag1 ("TAG_1"); const std::s

嗨,我在运行下面的代码时看到这个错误。“分割错误(核心转储)”我不能确定原因。\n有人帮帮我。谢谢

#ifndef __LIB_TAG_STRINGS_H__
#define __LIB_TAG_STRINGS_H__

//============
//stl
#include <deps.h>
#include <string>
namespace tags
 {
   const std::string tag1 ("TAG_1");
   const std::string tag2 ("TAG_2");
 }//END namespace TAGS

namespace attributes
 {
  const std::string attribute1 ("ATTRIBUTE_1");
  const std::string attribute2("ATTRIBUTE_2");
 }

class _Name 
 {
  public:

  _Name ()
   {
    /**This constructor is used to map some tags with the strings**/
    string_map.insert (std::make_pair (std::string ("TAGNOTE1"), tags::tag1 ));
    string_map.insert (std::make_pair (std::string ("TAGNOTE2"), tags::tag2 ));
   };

 const std::string& getName (const std::string& class_name) const
   {   
    std::map<std::string, std::string>::const_iterator i = string_map.find(class_name);
    return (i != string_map.end ()) ? i->second : null_string;
  }


   private:
    std::map<std::string, std::string> string_map;
    std::string null_string;
 };

namespace Name
 {
  const _Name NAME;
 }
#endif
\ifndef\uuu LIB\u TAG\u STRINGS\uh__
#定义\uuuuLib\uTag\uStrings\uH__
//============
//stl
#包括
#包括
名称空间标记
{
常量std::字符串tag1(“TAG_1”);
常量std::字符串tag2(“TAG_2”);
}//结束命名空间标记
名称空间属性
{
常量std::字符串attribute1(“ATTRIBUTE_1”);
常量std::字符串attribute2(“ATTRIBUTE_2”);
}
类名
{
公众:
_姓名()
{
/**此构造函数用于映射带有字符串的一些标记**/
string_map.insert(std::make_pair(std::string(“TAGNOTE1”),tags::tag1));
string_map.insert(std::make_pair(std::string(“TAGNOTE2”),tags::tag2));
};
const std::string和getName(const std::string和class_name)const
{   
std::map::const\u迭代器i=string\u map.find(class\u name);
返回(i!=string\u map.end())?i->second:null\u字符串;
}
私人:
std::映射字符串\u映射;
std::string null_string;
};
名称空间名称
{
常数名称;
}
#恩迪夫
内核在运行时被转储。这是GDB指向错误的代码。

GDB日志:

Program received signal SIGSEGV, Segmentation fault. 0x0000003b00e9d23b in std::basic_string, std::allocator >::basic_string(std::basic_string, std::allocator > const&) () from /usr/lib64/libstdc++.so.6 Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.80.el6.x86_64 libaio-0.3.107-10.el6.x86_64 libgcc-4.4.6-4.el6.x86_64 libstdc++-4.4.6-4.el6.x86_64 nss-softokn-freebl-3.12.9-11.el6.x86_64 openssl-1.0.0-20.el6_2.5.x86_64 zlib-1.2.3-27.el6.x86_64 (gdb) where 0 0x000strong text0003b00e9d23b in std::basic_string, std::allocator >::basic_string(std::basic_string, std::allocator > const&) () from /usr/lib64/libstdc++.so.6 1 0x00007ffff1375372 in _Name::_Name (this=0x7ffff15a9b60) at /prog/lib/tag_strings.h:229 2 0x00007ffff139581b in __static_initialization_and_destruction_0 () at /prog/lib/tag_strings.h:257 3 global constructors keyed to dest.cxx(void) () at dest.cxx:679 4 0x00007ffff1398516 in __do_global_ctors_aux () from ./dest.so 5 0x00007ffff135a2bb in _init () from ./dest.so 6 0x00007fffe361f000 in ?? () 7 0x0000003af860e535 in _dl_init_internal () from /lib64/ld-linux-x86-64.so.2 8 0x0000003af8600b3a in _dl_start_user () from /lib64/ld-linux-x86-64.so.2 9 0x0000000000000001 in ?? () 10 0x00007fffffffe128 in ?? (). 11 0x0000000000000000 in ?? (). 程序接收信号SIGSEGV,分段故障。 std::basic_字符串中的0x0000003b00e9d23b,std::allocator>::basic_字符串(std::basic_字符串,std::allocator>const&)()来自/usr/lib64/libstdc++.so.6 缺少单独的调试信息,请使用:debuginfo安装glibc-2.12-1.80.el6.x86_64 libaio-0.3.107-10.el6.x86_64 libgcc-4.4.6-4.el6.x86_64 libstdc++-4.4.6-4.el6.x86_64 nss-softokn-freebl-3.12.9-11.el6.x86_64 openssl-1.0.0-20.el6_2.5.x86_64 zlib-1.2.2.6-elu 64 (gdb)在哪里 0 0x000strong text0003b00e9d23b在std::basic_字符串中,std::allocator>::basic_字符串(std::basic_字符串,std::allocator>const&(),来自/usr/lib64/libstdc++.so.6 1 0x00007ffff1375372 in _Name::_Name(this=0x7ffff15a9b60) at/prog/lib/tag_strings.h:229 2 0x00007ffff139581b位于静态初始化和销毁0() at/prog/lib/tag_strings.h:257 3个全局构造函数在dest.cxx:679处键入dest.cxx(void)() 4 0x00007ffff1398516 in_uuuudo_uglobal_uctors_uaux()from./dest.so 5 0x00007ffff135a2bb在_init()中从./dest.so 6 0x00007fffe361f000英寸??() 7 0x0000003af860e535,位于/lib64/ld-linux-x86-64.so.2中的_dl_init_internal()中 8 0x0000003af8600b3a位于/lib64/ld-linux-x86-64.so中的_dl_start_user()中 9 0x0000000000000001英寸??() 10 0x00007FFFFFE128英寸??(). 11 0x0000000000000000英寸??().
在我看来,这是一个静态初始化顺序问题

当您的名称对象被构造时,它访问tag1/tag2对象——但不能保证这些对象已经被构造

您可以尝试将依赖的对象更改为函数

namespace tags {
    static std::string tag1() { return std::string("TAG1"); }
    ...
}

...
string_map.insert (std::make_pair (std::string ("TAGNOTE1"), tags::tag1() ));
...

嗨,我在运行下面的代码时看到这个错误。“分割错误(核心转储)”我不能确定原因。\n有人帮帮我。谢谢这里是
main()
?什么是
?好的,GDB会告诉您崩溃的位置。您介意告诉我们崩溃在哪里吗?@AndyProwl deps.h包含包含的头文件列表实际上std::basic_字符串中的崩溃与我的答案中的解释完全一致:-)非常感谢@hmn代码现在正在工作。我想问一下,为什么前面的代码在solaris上运行得很好。tag1、tag2和NAME的初始化顺序是未定义的-您可以将其视为随机的。有时,重新编译代码以使这种崩溃(暂时)消失已经足够了。