Problems with CFileDialog instantiation

自闭症网瘾萝莉.ら 提交于 2019-12-19 08:18:11

问题


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?


回答1:


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.




回答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!



来源:https://stackoverflow.com/questions/27049481/problems-with-cfiledialog-instantiation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!