[英]Why do I get "argument of type "LPCSTR" is incompatible with parameter of type "LPCWSTR" error when code compiles fine?
[英]Argument of type “LPCWSTR” is incompatible of the parameter of type “LPCSTR”
有人可以告訴我為什么我會出錯。 我一直試圖弄清楚這一點。
LPCWSTR drive2[4] = { L"C:\\", L"D:\\", L"E:\\", L"F:\\" };
int i;
UINT test;
for (i = 0; i<12; i++)
{
test = GetDriveType(drive2[i]); //anything from here with "drive2[i]" is an error.
switch (test)
{
case 0: ::MessageBox(Handle, drive2[i], "0 cannot be determined", MB_OK);
break;
case 1: ::MessageBox(Handle, drive2[i], "1 invalid", MB_OK);
break;
case 2: ::MessageBox(Handle, drive2[i], "2 removable", MB_OK);
break;
case 3: ::MessageBox(Handle, drive2[i], "3 fixed", MB_OK);
break;
default: "Unknown value!\n";
謝謝!
LPCWSTR
是const wchar_t*
的別名。
您正在使用GetDriveType()
和MessageBox()
函數的TCHAR
版本。 如果UNICODE
在編譯時定義,則TCHAR
映射到wchar_t
,否則映射到char
。
您的drive2
變量是一個wchar_t
指針數組,因此為了將drive2[i]
按原樣傳遞給GetDriveType()
和MessageBox()
,您必須為 Unicode 編譯您的項目(即,使UNICODE
有條件地在編譯時定義-time),這將使GetDriveType()
map 到GetDriveTypeW()
和MessageBox()
map 到MessageBoxW()
以便它們只接受寬( wchar_t
) 字符串。 否則, GetDriveType()
將 map 到GetDriveTypeA()
和MessageBox()
將 map 到MessageBoxA()
所以它們只接受窄( char
)字符串。
您正在將窄字符串文字傳遞給MessageBox()
,這在為 Unicode 編譯時將不起作用。 如果您不為 Unicode 編譯,則不能將寬字符串傳遞給TCHAR
函數 - 這聽起來像您的情況,因為錯誤消息抱怨將const wchar_t*
指針傳遞給const char*
參數。
在定義UNICODE
時,您需要使用TEXT()
宏來使字符串文字變寬,而不是變窄。
我還建議將TEXT()
用於您的drive2
數組中的字符串文字,以匹配您將數組元素傳遞給的TCHAR
函數。
此外,您的循環超出了drive2
數組的范圍。
話雖如此,試試這個:
LPCTSTR drive2[4] = { TEXT("C:\\"), TEXT("D:\\"), TEXT("E:\\"), TEXT("F:\\") };
int i;
UINT test;
for (i = 0; i < 4; i++)
{
test = GetDriveType(drive2[i]);
switch (test)
{
case 0:
::MessageBox(Handle, drive2[i], TEXT("0 cannot be determined"), MB_OK);
break;
case 1:
::MessageBox(Handle, drive2[i], TEXT("1 invalid"), MB_OK);
break;
case 2:
::MessageBox(Handle, drive2[i], TEXT("2 removable"), MB_OK);
break;
case 3:
::MessageBox(Handle, drive2[i], TEXT("3 fixed"), MB_OK);
break;
default:
::MessageBox(Handle, drive2[i], TEXT("Unknown value!"), MB_OK);
break;
}
}
否則,如果你想專門處理wchar_t
(你應該這樣做),那么直接使用基於 Unicode 的 function 定義,並且只使用寬字符串文字,根本不要處理TCHAR
:
LPCWSTR drive2[4] = { L"C:\\", L"D:\\", L"E:\\", L"F:\\" };
int i;
UINT test;
for (i = 0; i < 4; i++)
{
test = GetDriveTypeW(drive2[i]);
switch (test)
{
case 0:
::MessageBoxW(Handle, drive2[i], L"0 cannot be determined", MB_OK);
break;
case 1:
::MessageBoxW(Handle, drive2[i], L"1 invalid", MB_OK);
break;
case 2:
::MessageBoxW(Handle, drive2[i], L"2 removable", MB_OK);
break;
case 3:
::MessageBoxW(Handle, drive2[i], L"3 fixed", MB_OK);
break;
default:
::MessageBoxW(Handle, drive2[i], L"Unknown value!", MB_OK);
break;
}
}
LPCSTR
是const char*
,而LPCWSTR
是const wchar_t*
,它們不兼容。
C++ 中的寬字符串聲明: LPCWSTR string = L"Wide string";
正則字符串: LPCSTR string = "Regular string";
如果你想使用寬字符串,你需要使用W
版本,在你的情況下,使用GetDriveTypeW
。
請注意, GetDriveType
不是 function,它是一個宏,如果您的代碼以 ASCII 模式(默認)編譯,則擴展為GetDriveTypeA
,但如果代碼以 UNICODE 模式編譯,則擴展為GetDriveTypeW
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.