我在这些代码行上遇到奇怪的分段错误。
OutputHandler(std::string const& path) throw(std::exception);
MyClass::MyClass()
: basepath(boost::filesystem::canonical("./").string() + "/"),
outputHandler(basepath + "log.log") {} // Error traces back to this line (line 11)
分段错误:
#0 0x00007ffff6fd512b in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1 0x0000000000423bd7 in std::operator+<char, std::char_traits<char>, std::allocator<char> > (__lhs=..., __rhs=0x4659a1 "log.log")
at /usr/include/c++/4.8/bits/basic_string.h:2405
#2 0x0000000000414466 in MyClass::MyClass (this=0x7fffffffd6f0) at ./source/MyClass.cpp:11
#3 0x0000000000464a28 in main () at ./source/main.cpp:10
导致此错误的原因是什么?如何解决?我在猜测将basepath + "log.log"
转换为std::string
时出错了?
答案 0 :(得分:3)
成员变量的初始化顺序是它们在类定义中声明的顺序,而不是它们在初始化程序中列出的顺序。 basepath
字符串可能尚未初始化。