I am getting an odd segmentation error on these lines of code.

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)

The segmentation error:

#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

What is causing this error and how do I fix it? I am guessing something is going wrong when converting the basepath + "log.log" to a std::string?

有帮助吗?

解决方案

The order of initialization of member variables is the order they're declared in the class definition, not the order they're listed in the initializer. The basepath string is probably not initialized yet.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top