如何调用CFileDialog类的构造函数

时间:2013-07-29 07:37:39

标签: visual-studio-2010 visual-c++ constructor mfc cfiledialog

我需要在类的所有成员函数中使用CFileDialog对象。所以我把类的标题写成:

#pragma once
#include <string.h>
#include "afxdlgs.h"
#include "gdal_priv.h"
#include "cpl_conv.h"
#include "cpl_string.h"
#include "ogr_spatialref.h"


class FilesWorkFlow
{
public:
    FilesWorkFlow(void);
    virtual ~FilesWorkFlow(void);
    CString GetPath();
    GDALDataset* OpenTiff(CString);
private:
    wchar_t* lpszFilter;
    CFileDialog dlgFile;
};  

以这种形式实现了类的构造函数:

FilesWorkFlow::FilesWorkFlow(void)
{
    lpszFilter = _T("JPEG Files (*.jpg)|*.jpg|")
    _T("TIFF Files (*.tif)|*.tif|")_T("PNG Files (*.png)|*.png|")_T("Bitmap Files (*.bmp)|*.bmp|");

    dlgFile = CFileDialog(true,0,0,OFN_ENABLESIZING | OFN_HIDEREADONLY,lpszFilter,0,0,true);
}  

但在构造函数中,我收到错误:

no default constructor exists for class CFileDialog  

有什么问题?

----------------------------------------------- --------------------------------------------

问题解决了。我用这种方式实现了这个类。

FilesWorkFlow.h

class FilesWorkFlow
{
public:
    FilesWorkFlow(void);
    virtual ~FilesWorkFlow(void);
    CString GetPath();
    GDALDataset* OpenTiff(CString);
private:
    wchar_t* lpszFilter;
    CFileDialog* dlgFile;
};  

FilesWorkFlow.cpp

FilesWorkFlow::FilesWorkFlow(void)
{
        lpszFilter = _T("JPEG Files (*.jpg)|*.jpg|")
    _T("TIFF Files (*.tif)|*.tif|")_T("PNG Files (*.png)|*.png|")_T("Bitmap Files (*.bmp)|*.bmp|");
        dlgFile = new CFileDialog(true,0,0,OFN_ENABLESIZING | OFN_HIDEREADONLY,lpszFilter,0,0,true);
}


FilesWorkFlow::~FilesWorkFlow(void)
{
}


CString FilesWorkFlow::GetPath()
{
    if (dlgFile->DoModal() == IDOK)
    {
        CString pathname = dlgFile->GetPathName();
        return pathname;
    }
 }

2 个答案:

答案 0 :(得分:1)

试试这个:

FilesWorkFlow::FilesWorkFlow(void)
{
    lpszFilter = _T("JPEG Files (*.jpg)|*.jpg|")
    _T("TIFF Files (*.tif)|*.tif|")_T("PNG Files (*.png)|*.png|")_T("Bitmap Files (*.bmp)|*.bmp|");

    CFileDialog *dlgFile = new CFileDialog(true,0,0,OFN_ENABLESIZING | OFN_HIDEREADONLY,lpszFilter,0,0,true);
    ...
    delete dlgfile ;
}

FilesWorkFlow::FilesWorkFlow(void)
{
    lpszFilter = _T("JPEG Files (*.jpg)|*.jpg|")
    _T("TIFF Files (*.tif)|*.tif|")_T("PNG Files (*.png)|*.png|")_T("Bitmap Files (*.bmp)|*.bmp|");

    CFileDialog dlgFile(true,0,0,OFN_ENABLESIZING | OFN_HIDEREADONLY,lpszFilter,0,0,true);
    ...
}

答案 1 :(得分:1)

专门化您通常从中派生的CFileDialog,而不是使其成为类成员。但是,无论您做什么,解决方案都是通过初始化列表初始化CFileDialog对象。你的构造函数看起来像这样:

FilesWorkFlow::FilesWorkFlow(void) :
    dlgFile( true, 0, 0,
             OFN_ENABLESIENABLESIZING | OFN_HIDEREADONLY,
             _T("JPEG Files (*.jpg)|*.jpg|TIFF Files (*.tif)|*.tif|PNG Files (*.png)|*.png|Bitmap Files (*.bmp)|*.bmp||"),
             0, 0, true )
{
}