0
votes

I am trying to a "File Save As" programming code inside MFC Application.

in my TestDlg.h header file i have got

public:
    BOOL SaveFile (LPCTSTR pszFile);
    CString m_strPathName;

And in my TestDlg.cpp CPP file I have got

void CTESTDlg::OnSaveFile()
{
    TCHAR szFilters[] =
    _T ("Text files (*.txt)¦*.txt¦All files (*.*)¦*.*¦¦");

    CFileDialog dlg (FALSE, _T ("txt"), _T ("*.txt"),
    OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY, szFilters);

    if (dlg.DoModal () == IDOK) 
    {
        if (SaveFile (dlg.GetPathName ()))
            m_strPathName = dlg.GetPathName ();
    }
}

After this, I build my solution and got this error.

LNK2019: unresolved external symbol "public: int __thiscall CTESTDlg::SaveFile(wchar_t const *)" (?SaveFile@CTESTDlg@@QAEHPB_W@Z) referenced in function "public:

How do I resolve this?? Help is much appreciated. Thank you.

EDIT.

After removing the if (SaveFile (dlg.GetPathName ()) line, the file can build and run but when ever I press the save button, no file is saved.

2
Did you implement SaveFile? Are you compiling the cpp? - Luchian Grigore
@LuchianGrigore The SaveFile is implement inside the header file. - Ashton
Can you show more code? Ideally a short, self-contained example which demonstrates your problem and that other people could try building. - simonc
BOOL SaveFile (LPCTSTR pszFile); - this is not implementation. - Alex F

2 Answers

1
votes

It not any MFC or internal error its your programming error.

When you declare any method in .h file body of that method should be present in .cpp file.else it will give an linking error of function is not found in .obj file. So your solution is that use same function in .h and .cpp file like, In .h file,

public:
    BOOL SaveFile (LPCTSTR pszFile);
    CString m_strPathName;'

and in .cpp file,

void CTESTDlg::OnSaveFile(LPCTSTR pszFile)
{
    TCHAR szFilters[] =
    _T ("Text files (*.txt)¦*.txt¦All files (*.*)¦*.*¦¦");

    CFileDialog dlg (FALSE, _T ("txt"), _T ("*.txt"),
    OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY, szFilters);

    if (dlg.DoModal () == IDOK) 
    {
        if (SaveFile (dlg.GetPathName ()))
            m_strPathName = dlg.GetPathName ();
    }
}
0
votes

Add this to your CPP file:

BOOL CTESTDlg::SaveFile (LPCTSTR pszFile)
{
    // ... add your code that saves the information to the file here...

    return TRUE;
}

It still won't do anything (there's no save code) but it will compile.