C++删除函数:不接受字符串变量

C++删除函数:不接受字符串变量,c++,C++,两个例子: 当我这样做时: string filename = "apple"; remove(filename+".txt"); 但它会产生一个错误 string filename = "apple"; remove("apple.txt"); 没有。为什么它不起作用,为什么我可以使用,例如: string filename = "apple"; ifstream apple (filename+".txt"); 假设文件名的类型为std::string,我可以想出几种解决问题的方法 在

两个例子: 当我这样做时:

string filename = "apple";
remove(filename+".txt");
但它会产生一个错误

string filename = "apple";
remove("apple.txt");
没有。为什么它不起作用,为什么我可以使用,例如:

string filename = "apple";
ifstream apple (filename+".txt");
假设文件名的类型为std::string,我可以想出几种解决问题的方法

在评论中使用@Cornstales的建议:

remove((filename+".txt").c_str());
在应用程序的命名空间中创建一个助手函数并使用它

namespace myapp
{
    int remove(std::string const& filename)
    {
        return std::remove(filename.c_str());
    }
}

myapp::remove(filename+".txt");

什么类型的文件名?将文件名设为std::string。什么是删除?你知道这个函数需要一个常量字符*,是吗?尝试removefilename+.txt.c_str假设文件名是std::string文件名是char*还是std::string?如果它不工作,程序是否运行?投票需要15个信誉度。谢谢第一个成功了,不用担心。如果我的回答能帮助你解决问题,那就好了。