不存在从“std::string”到“const char *”的合适的转换函数 c++

mr.*_*tr0 -1 c++

这是一个用于版本号目录的简单程序。它无法编译,出现此错误:

不存在从“std::string”到“const char *”的合适转换函数

#include <iostream>
#include <stdio.h>
#include <io.h>

using namespace std;

int main() {
    int max = 10, daily_patch = 0, monthly_patch = 0, yearly_patch;

    cout << " Enter the yearly_patch Number : ";
    cin >> yearly_patch;

    for (int i = yearly_patch; i <= yearly_patch; i++) {
        for (int j = 0; j < max; j++) {
            for (int k = 0; k < max; k++) {
                string str1 = to_string(i);
                string str2 = to_string(j);
                string str3 = to_string(k)
                string dot = ".";
                string dir = str1 + dot + str2 + dot + str3;
                
                mkdir(dir);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Bil*_*nch 6

mkdir()来自 POSIX API,它需要 C 语言,所以它需要一个 c 样式的字符串。所以用mkdir(dir.c_str());

如果你想使用 C++ API 并且你有一个最新的编译器,你可以使用std::filesystem::create_directory(dir);.

  • mkdir 来自 C api。所以它不理解 C++ 对象。如果您想要 C++ api,请使用 https://en.cppreference.com/w/cpp/filesystem/create_directory (4认同)