CFileDialog实例化的问题 [英] Problems with CFileDialog instantiation

查看:401
本文介绍了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] \\Users\Jonathan\Documents\Visual Studio 2013\Projects\SDI\SDI\MainFrm.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:\Users\Jonathan\Documents\Visual Studio 2013\Projects\SDI\SDI\MainFrm.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函数将使用宽字符。因此,使用 char char * const char * 在Windows API函数中,希望宽字符串无法编译。

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.

这篇关于CFileDialog实例化的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆