【问题标题】:error: cannot pass objects of non-trivially-copyable type 'class boost::filesystem::path' through '...'错误:不能通过'...'传递非平凡可复制类型'class boost::filesystem::path'的对象
【发布时间】:2019-03-15 16:51:58
【问题描述】:

错误:

init.cpp:在函数'bool AppInit2()'中:

init.cpp:608:83: 错误:无法通过“...”传递非平凡可复制类型“class boost::filesystem::path”的对象

printf("正在创建 %s 的备份 -> %s\n", sourceFile, backupFile);

init.cpp:608:83: 错误:不能通过“...”传递非平凡可复制类型“class boost::filesystem::path”的对象

init.cpp:643:76:错误:无法将参数 '2' 的 'boost::filesystem::path' 转换为 'const char*' 到 'int sprintf(char*, const char*, ... )'

sprintf("旧备份已删除:%s\n", file.second);

make: *** [obj/init.o] 错误 1

filesystem::path backupDir = GetDataDir() / "backups";
if (!filesystem::exists(backupDir))
{
    // Always create backup folder to not confuse the operating system's file browser
    filesystem::create_directories(backupDir);
}
nWalletBackups = GetArg("-createwalletbackups", 10);
nWalletBackups = std::max(0, std::min(10, nWalletBackups));
if(nWalletBackups > 0)
{
    if (filesystem::exists(backupDir))
    {
        // Create backup of the wallet
        std::string dateTimeStr = DateTimeStrFormat(".%Y-%m-%d-%H.%M", GetTime());
        std::string backupPathStr = backupDir.string();
        backupPathStr += "/" + strWalletFileName;
        std::string sourcePathStr = GetDataDir().string();
        sourcePathStr += "/" + strWalletFileName;
        boost::filesystem::path sourceFile = sourcePathStr;
        boost::filesystem::path backupFile = backupPathStr + dateTimeStr;
        sourceFile.make_preferred();
        backupFile.make_preferred();
        try {
            boost::filesystem::copy_file(sourceFile, backupFile);
            printf("Creating backup of %s -> %s\n", sourceFile, backupFile);
        } catch(boost::filesystem::filesystem_error &error) {
            printf("Failed to create backup %s\n", error.what());
        }
        // Keep only the last 10 backups, including the new one of course
        typedef std::multimap<std::time_t, boost::filesystem::path> folder_set_t;
        folder_set_t folder_set;
        boost::filesystem::directory_iterator end_iter;
        boost::filesystem::path backupFolder = backupDir.string();
        backupFolder.make_preferred();
        // Build map of backup files for current(!) wallet sorted by last write time
        boost::filesystem::path currentFile;
        for (boost::filesystem::directory_iterator dir_iter(backupFolder); dir_iter != end_iter; ++dir_iter)
        {
            // Only check regular files
            if ( boost::filesystem::is_regular_file(dir_iter->status()))
            {
                currentFile = dir_iter->path().filename();
                // Only add the backups for the current wallet, e.g. wallet.dat.*
                if(currentFile.string().find(strWalletFileName) != string::npos)
                {
                    folder_set.insert(folder_set_t::value_type(boost::filesystem::last_write_time(dir_iter->path()),

*dir_iter)); } } } // 向后循环备份文件并保留 N 个最新的 (1 nWalletBackups) { // 多于 nWalletBackups 备份:删除最旧的备份 尝试 { boost::filesystem::remove(file.second); sprintf("旧备份已删除:%s\n", file.second); } 捕捉(提升::文件系统::文件系统错误和错误){ sprintf("删除备份 %s 失败\n", error.what()); } } } } }

【问题讨论】:

  • prinf() 不能用于打印boost::filesystem::path 的实例。您需要使用std::cout 而不是printf(),或者从boost::filesystem::path 实例中提取const chat*
  • @πάνταῥεῖ 我这样说并编译: 第 608 行:std::cout %s\n", sourceFile, backupFile;第 643 行:std::cout
  • 这不是std::ostream&amp; operator&lt;&lt;(std::ostream%, const T&amp;) 运算符的使用方法。没有使用类型占位符,您的声明应该看起来像 std::cout &lt;&lt; "Creating backup of" &lt;&lt; sourceFile &lt;&lt; "-&gt;" &lt;&lt; backupFile &lt;&lt; '\n';
  • @πάνταῥεῖ 我说的方法有效!该文件通常是使用日期和时间参数生成的。
  • 您还在终端收到了预期的输出?我对此表示怀疑。提示,请阅读 逗号运算符 的作用。

标签: c++ boost


【解决方案1】:

朋友的@πάνταῥεῖ 回答解决了我的问题!

std::cout << "Creating backup of" << sourceFile << "->" << backupFile << '\n';

std::cout << "Old backup deleted:" << file.second << '\n';

非常感谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多