[英]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 Set
是Use 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.