CFileDialog 实例化的问题
我遵循 CFileDialog
的定义,但 VS2013 仍然告诉我没有构造函数可用于我传入的参数.
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.
我的代码:
CFile theFile;
char strFilter[] = { "TXT Files (*.txt)|*.txt|All Files (*.*)|*.*||" };
CFileDialog fDlg = CFileDialog(TRUE, ".txt", NULL, 0, strFilter);
产生的错误:
1 IntelliSense:没有构造函数CFileDialog::CFileDialog"的实例与参数列表匹配参数类型为: (int, const char [5], int, int, char [46]) c:UsersJonathanDocumentsVisual Studio 2013ProjectsSDISDIMainFrm.cpp 131 21 SDI
1 IntelliSense: no instance of constructor "CFileDialog::CFileDialog" matches the argument list argument types are: (int, const char [5], int, int, char [46]) c:UsersJonathanDocumentsVisual Studio 2013ProjectsSDISDIMainFrm.cpp 131 21 SDI
以及CFileDialog
构造函数供参考:
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);
有什么问题?
推荐答案
问题似乎是您使用了不正确的字符串类型.
The issue seems to be that you're using the incorrect string type.
快速的解决方案是使用 TCHAR
而不是 char
.更好的解决方案是只使用宽字符串并确保构建是 Unicode.
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.
在 Visual Studio 中创建项目时,使用的默认字符集类型是 Unicode,而不是 MBCS,也不是未设置".这意味着采用字符数组和指针的 Windows API 和 MFC 函数将使用宽字符.因此,在需要宽字符串的 Windows API 函数上使用 char
、char *
、const char*
将无法编译.
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.
即使您对 Unicode 或 MBCS 一无所知,也表明您的代码有误,即您调用的函数采用 LPCTSTR
类型――即 不是 一个 const char *
,它就是这样,即一个指向 TCHAR
的常量指针.如果您坚持知道使用指定的类型,那么您会很高兴.
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.
相关文章