4

I'm following the definition for CFileDialog, yet VS2013 is still telling me that there is no constructor available for the arguments that I'm passing in.

My Code:

CFile theFile;
char strFilter[] = { "TXT Files (*.txt)|*.txt|All Files (*.*)|*.*||" };
CFileDialog fDlg = CFileDialog(TRUE, ".txt", NULL, 0, strFilter);

Resulting Error:

1 IntelliSense: no instance of constructor "CFileDialog::CFileDialog" matches the argument list argument types are: (int, const char [5], int, int, char [46]) c:\Users\Jonathan\Documents\Visual Studio 2013\Projects\SDI\SDI\MainFrm.cpp 131 21 SDI

And the CFileDialog constructor for reference:

explicit CFileDialog(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
    LPCTSTR lpszDefExt = NULL,
    LPCTSTR lpszFileName = NULL,
    DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
    LPCTSTR lpszFilter = NULL,
    CWnd* pParentWnd = NULL,
    DWORD dwSize = 0,
    BOOL bVistaStyle = TRUE);

What's the problem?

JayB
  • 397
  • 6
  • 21
  • 2
    Is this a UNICODE build or MBCS? – PaulMcKenzie Nov 20 '14 at 21:07
  • @PaulMcKenzie I'm not sure how to answer that question. It's a blank slate MFC program, this is the first bit of code I've entered in to a file->open event handler – JayB Nov 20 '14 at 21:08
  • @user134788 prefix your string literals with `L` and try again. You'll have to change the type of `strFilter` to `WCHAR`. If you're married to the idea of using the T-macros, surround your literals with `_T()` and use `TCHAR` for the type of `strFilter[]` – WhozCraig Nov 20 '14 at 21:09

2 Answers2

5

The issue seems to be that you're using the incorrect string type.

The quick solution is to use TCHAR and not char. The better solution is to just use wide strings and make sure the build is Unicode.

When you create a project in Visual Studio, the default character set type that is used is Unicode, not MBCS and not "Not Set". This means that Windows API and MFC functions that take character arrays and pointers will be using wide characters. Therefore using char, char *, const char*, on Windows API functions that expect wide strings will not compile.

The indication that your code is wrong, even if you knew nothing about Unicode or MBCS, is that the functions you're calling take types of LPCTSTR -- that is not a const char *, it is what it is, namely a constant pointer to a TCHAR. If you stuck with knowing to use the types specified, you would have been good to go.

So the lesson is that if a function wants a type, provide a variable or expression of that type, not what you think the type is equivalent to.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
2

Ok. So I've changed my code to the following:

CFile theFile;
TCHAR strFilter[] = { _T("TXT Files (*.txt)|*.txt|All Files (*.*)|*.*||") };
CFileDialog fDlg = CFileDialog(TRUE, _T(".txt"), NULL, 0, strFilter);

And, there are now no problems. Thank you for your responses!

JayB
  • 397
  • 6
  • 21
  • 1
    Another things with TCHAR is that you should not use `L"..."` for strings or even `"..."` You should use `_T("...")`. This is because a TCHAR can be either a `char` or `WCHAR`. – rafeek Nov 20 '14 at 21:39