【发布时间】:2009-05-13 17:21:20
【问题描述】:
在这个 C++ 中一定有一些我没有意识到的明显的东西。
load(string & filename){
string command;
char delimiter = '/';
size_t delimiterPos = filename.rfind(delimiter);
string directory = string(filename.c_str(),delimiterPos);
command = "import path ";
//want to add directory to end of command
string temp = command + "hello!"; // This works
command.append(directory); //This works!
command += directory; //This seg faults!
...
}
在 GDB 中,当我在函数开头“打印”文件名时,我得到: (const string &) @0x9505f08: {静态 npos = 4294967295, _M_dataplus = {> = {<:new_allocator> = {}, }, _M_p = 0x950a8e4 "../config/pythonFile.py"}}
这到底是怎么回事,文件名的格式如何不正确,这样 .append() 有效而 += 无效?! C++ 中的重载函数 += 有什么奇怪的吗?
g++ 版本 3.4.6
【问题讨论】:
-
进入 operator+= 函数以查看它实际崩溃的行。
-
如果颠倒 append 和 += 的调用顺序会发生什么?还是您一次尝试一个?
-
@harper 死于 += 并且永远不会到达 .append
-
我试用了你的代码,它就像一个魅力。你用的是哪个编译器?
-
另外,试试:string temp = "hello";命令 += 温度;这行得通吗?