简体   繁体   English

无法转换 'std::__cxx11::string {aka std::__cxx11::basic_string<char> }' 到 'LPCSTR {aka const char*}'</char>

[英]cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'LPCSTR {aka const char*}'

#include <iostream>
#include <string>
#include <sstream>
#include <windows.h>
#include <vector>

using namespace std;

int main() {
    string PNGFilePath, WEBPFilePath;
    int number, c;
    char title[256];

    cout << "Enter a Number: ";
    cin >> number;
    cout << endl;

    cout << "Title: ";
    cin.getline(title, 256, ';');

    cout << "Enter PNG directory: ";
    cin >> PNGFilePath;
    cout << endl;

    cout << "Enter WEBP directory: ";
    cin >> WEBPFilePath;
    cout << endl;

    std::string OldPNGFolder = std::string(PNGFilePath + "\\");
    c = 1;

    while (title[c] != '\0') {
        OldPNGFolder += title[c];
        c++;
    }

    std::string NewPNGFolder = std::string(PNGFilePath + "\\[");
    c = 1;
    NewPNGFolder += to_string(number);
    NewPNGFolder += "]";
    while (title[c] != '\0') {
        NewPNGFolder += title[c];
        c++;
    }

    MoveFile(OldPNGFolder, NewPNGFolder);
}

I tried adding "(OldPNGFolder.c_str()" and it still shows the same error message, also tried system(OldPNGFolder.c_str()); and still the same message.我尝试添加“(OldPNGFolder.c_str()”,它仍然显示相同的错误消息,也尝试了 system(OldPNGFolder.c_str()); 仍然是相同的消息。

Adding "LPCTSTR" shows the error "error: expected primary-expression before 'OldPNGFolder' MoveFile(LPCTSTR OldPNGFolder, NewPNGFolder);"添加“LPCTSTR”显示错误“错误:'OldPNGFolder'MoveFile(LPCTSTR OldPNGFolder,NewPNGFolder)之前的预期主表达式;”

Is there a way to fix this???有没有办法来解决这个问题???

Do you use Visual Studio?你使用 Visual Studio 吗?
If your project's Character Set is Use Unicode Character Set in the Project's Property window, MoveFile means that MoveFileW.如果你的项目的Character SetUse Unicode Character Set in the Project's Property window,MoveFile就是MoveFileW.
It's parameter type is LPCTSTR, that is const wchar_t *, not the const char *.它的参数类型是LPCTSTR,也就是const wchar_t *,不是const char *。
Your error is not in converting from string to const char *, just in parameter type error in MoveFile.您的错误不在于从字符串转换为 const char *,而在于 MoveFile 中的参数类型错误。

You can fix this by use MoveFileA, MoveFileA(OldPNGFolder.c_str(), NewPNGFolder.c_str());您可以使用 MoveFileA, MoveFileA(OldPNGFolder.c_str(), NewPNGFolder.c_str());修复此问题or by convert string to wstring or LPCWSTR as below.或者如下所示将字符串转换为 wstring 或 LPCWSTR。

wstring a2w(std::string & string_a)
{
    int length = MultiByteToWideChar(CP_UTF8, 0, string_a.c_str(), -1, NULL, 0);

    wchar_t* temp = new wchar_t[length];
    MultiByteToWideChar(CP_UTF8, 0, string_a.c_str(), -1, temp, length);

    wstring string_w = temp;
    delete[] temp;
    return string_w;
}

int main() {
    ...
    MoveFile(a2w(OldPNGFolder).c_str(), a2w(NewPNGFolder).c_str());
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取“错误:无法转换 'std::__cxx11::string* {aka std::__cxx11::basic_string<char> *}' 到 'const char*' 错误,同时将路径作为参数传递</char> - Getting "error: cannot convert 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' to 'const char*' error while passing a path as argument 'operator*' 不匹配(操作数类型为 'const string' {aka 'const std::__cxx11::basic_string<char> '})</char> - no match for 'operator*' (operand type is 'const string' {aka 'const std::__cxx11::basic_string<char>'}) 错误:传递&#39;const字符串{aka const std :: __ cxx11 :: basic_string <char> }&#39;作为&#39;this&#39;参数 - Error: passing ‘const string {aka const std::__cxx11::basic_string<char>}’ as ‘this’ argument 尝试生成随机字母错误:无法转换 'std::__cxx11::string {aka std::__cxx11::basic_string<char> }' 到 'char' 在分配</char> - Trying to generate a random letter error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'char' in assignment 字符串未正确复制并显示错误:无法转换 'std::__cxx11::string {aka std::__cxx11::basic_string<char> }'</char> - string not copying correctly and showing error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' c++ 无法将 'std::string' {aka 'std::__cxx11::basic_string'} 转换为 'std::string (*)[3]' {aka 'std::__cxx11::basic_string (*)[3] '} - c++ cannot convert ‘std::string’ {aka ‘std::__cxx11::basic_string’} to ‘std::string (*)[3]’ {aka ‘std::__cxx11::basic_string (*)[3]’} 错误:为&#39;operator std::string {aka std::__cxx11::basic_string 指定的返回类型<char> }&#39; - Error:return type specified for 'operator std::string {aka std::__cxx11::basic_string<char>}' 不匹配调用 '(std::string {aka std::__cxx11::basic_string<char> }) ()'</char> - no match for call to '(std::string {aka std::__cxx11::basic_string<char>}) ()' OSX“错误:无法转换&#39;const std :: __ cxx11 :: basic_string”是什么意思 <char> “ 意思? - OSX What does “error: cannot convert 'const std::__cxx11::basic_string<char>” mean? (std::__cxx11::string) [T = std::__cxx11::basic_string<char> ; std::__cxx11::string = std::__cxx11::basic_string<char> ] 不能重载 - (std::__cxx11::string) [with T = std::__cxx11::basic_string<char>; std::__cxx11::string = std::__cxx11::basic_string<char>] cannot be overloaded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM