gpt4 book ai didi

c++ - 为什么在 boost 程序 (boost::filesystem) 中输出 "Bus error: 10"?

转载 作者:太空宇宙 更新时间:2023-11-04 14:14:57 26 4
gpt4 key购买 nike

我在问题 How to pass a string type path to boost::filesystem:path's constructor? 上成功编译了程序然而,输出结果比预期的要多。

Folder status: 0
Bus error: 10

第一行是唯一的预期结果。我不明白为什么要来第二个?

我通过 gdb 得到以下调试信息:

(gdb) run
Starting program: /Users/ZL/Desktop/mtfsg
Reading symbols for shared libraries ++++............................. done
Folder status: 0

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff5fc005a0
0x00007fff9046aab7 in std::basic_string<char, std::char_traits<char>,
std::allocator<char>>::~basic_string ()
(gdb) bt
#0 0x00007fff9046aab7 in std::basic_string<char, std::char_traits<char>,
std::allocator<char> >::~basic_string ()
#1 0x00000001000018dd in main () at m.cpp:12

最佳答案

最后,我在我的代码中找到了错误点:

在类 OSxFS(tfs.h 文件)中,方法“string ShowStatus()”的返回类型“string”不正确。此方法不需要返回值,因为它的功能只是在屏幕上打印目录(文件夹)状态。因此,我将“string”返回类型替换为“void”。

方法“string ShowStatus()”的原始代码

**string** ShowStatus()
{
boost::filesystem::file_status folderStatus =
boost::filesystem::status(mFolderPath);

try
{
cout<<"Folder status: "<<boost::filesystem::is_directory(folderStatus)<<endl;
}
catch(boost::filesystem::filesystem_error &e)
{
cerr<<"Error captured: "<<e.what()<<endl;
}
}

正确的代码:

**void** ShowStatus()
{
boost::filesystem::file_status folderStatus =
boost::filesystem::status(mFolderPath);

try
{
cout<<"Folder status: "<<boost::filesystem::is_directory(folderStatus)<<endl;
}
catch(boost::filesystem::filesystem_error &e)
{
cerr<<"Error captured: "<<e.what()<<endl;
}
}

关于c++ - 为什么在 boost 程序 (boost::filesystem) 中输出 "Bus error: 10"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12033816/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com