简体   繁体   中英

Incompatible with parameter of type "LPCWSTR"

#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"

To compile your code in Visual C++ you need to use Multi-Byte char WinAPI functions instead of Wide char ones.

Set Project -> Properties -> Advanced (or. General for older versions) -> Character Set option to Use Multi-Byte Character Set

also see the screenshot

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

MessageBox(NULL,"Dialog creation failed! Aborting..", "Error", MB_OK);

After casting to 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.

Another way to come by this issue, is to use the L macro in front of your string.

MessageBox(NULL, L"Dialog creation failed! Aborting..", L"Error", MB_OK);

See: What does the 'L' in front a string mean in C++?

or

L prefix for strings in C++

you can use 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

It will work for any settings:

#include <tchar.h>

MessageBox(NULL, _T("Dialog creation failed! Aborting.."), _T("Error"), MB_OK);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM