Question

I need a CFileDialog object to be used in all of the member functions of a class. So I wrote the header of class as:

#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;
};  

and implemented the constructor of the class in this form:

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);
}  

but in the constructor, I get the error:

no default constructor exists for class CFileDialog  

what is the problem?

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

Problem solved. I implemented the class this way.

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;
    }
 }
Was it helpful?

Solution

To specialize the CFileDialog you usually derive from it, instead of making it a class member. Regardless of what you do, though, the solution is to initialize the CFileDialog object through an initializer list. Your constructor would look like this:

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 )
{
}

OTHER TIPS

Try this:

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 ;
}

or

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);
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top