C++ 与“LPCWSTR”类型的参数不兼容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33001284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Incompatible with parameter of type "LPCWSTR"
提问by Bogdan Tkachenko
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <dos.h>
using namespace std;
class Dir
{
public:
char* cat;
Dir()
{
cout << "(C:/*)\n";
cat = new char[50];
cin >> cat;
}
void virtual ShowFiles()
{
}
};
class Inside : public Dir
{
public:
void virtual ShowFiles()
{
HANDLE hSearch;
WIN32_FIND_DATA pFileData;
hSearch = FindFirstFile(cat, &pFileData);
if (hSearch != INVALID_HANDLE_VALUE)
do
{
// if ((pFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
cout << pFileData.cFileName << "\n";
} while (FindNextFile(hSearch, &pFileData));
FindClose(hSearch);
}
};
int main()
{
Dir *obj1[2];
obj1[1] = new Inside;
obj1[1]->ShowFiles();
return 0;
}
So I have a program, I need to show with dynamic char cat all file in directory, but it is compilable in Borland C++ but in Visual Studio 15 + Resharper it doesn't work. Severity Code Description Project File Line Error (active) argument of type "char *" is incompatible with parameter of type "LPCWSTR"
所以我有一个程序,我需要用动态字符 cat 显示目录中的所有文件,但它可以在 Borland C++ 中编译,但在 Visual Studio 15 + Resharper 中它不起作用。严重性代码说明“char *”类型的项目文件行错误(活动)参数与“LPCWSTR”类型的参数不兼容
回答by Anton Malyshev
To compile your code in Visual C++ you need to use Multi-Byte char WinAPI functions instead of Wide char ones.
要在 Visual C++ 中编译代码,您需要使用多字节字符 WinAPI 函数而不是宽字符函数。
Set Project -> Properties -> General -> Character Setoption to Use Multi-Byte Character Set
设置项目 -> 属性 -> 常规 -> 字符集选项以使用多字节字符集
回答by M Shajeeh Mustafa
I actually found another way to resolve this error since above method did not work for me.
我实际上找到了另一种解决此错误的方法,因为上述方法对我不起作用。
I casted all my constant character strings with (LPCWSTR)
. The solution looks like this
Earlier
我用(LPCWSTR)
. 该解决方案看起来像这样
早些时候
MessageBox(NULL,"Dialog creation failed! Aborting..", "Error", MB_OK);
After casting to LPCWSTR
转换为 LPCWSTR 后
MessageBox(NULL, (LPCWSTR) "Dialog creation failed! Aborting..", (LPCWSTR) "Error", MB_OK);
So just copying the (LPCWSTR)
and pasting wherever this error was generated resolved all my errors.
因此,只需(LPCWSTR)
在生成此错误的任何位置复制并粘贴即可解决我的所有错误。
回答by Stefan
Another way to come by this issue, is to use the L
macro in front of your string.
解决此问题的另一种方法是L
在字符串前面使用宏。
MessageBox(NULL, L"Dialog creation failed! Aborting..", L"Error", MB_OK);
See: What does the 'L' in front a string mean in C++?
or
或者
回答by Li Kui
you can use wchar_t
你可以使用wchar_t
class Dir
{
public:
wchar_t* cat;
Dir()
{
wcout << "(C:/*)\n";
cat = new wchar_t[50];
wcin >> cat;
}
void virtual ShowFiles()
{
}
};
In Visual Studio 2013 and later, the MFC libraries for multi-byle character encoding (MBCS) will be provided as an add-on to Visual Studio
在Visual Studio 2013 及更高版本中,用于多字节字符编码 ( MBCS)的 MFC 库将作为 Visual Studio 的附加组件提供
回答by 8Observer8
It will work for any settings:
它适用于任何设置:
#include <tchar.h>
MessageBox(NULL, _T("Dialog creation failed! Aborting.."), _T("Error"), MB_OK);