I'm stumped
-
Ok, I am playing around with VC++ again :) and have only done a little work on a form and am already getting compiler errors X|. My guess is the error will be apparent to someone who uses VC++ often. This is what I have added:
void CTestDlg::OnShowFileDlg()
{CFileDialog cd(true); if(IDOK == cd.DoModal()) { FunWithFile(cd); }
}
void CTestDlg::FunWithFile(CFileDialog cd)
{MessageBox(\_T("Hello"), \_T("Test Message"), MB\_OK);
}
and I get the following error messages
--------------------Configuration: Test - Win32 Debug-------------------- Compiling... TestDlg.cpp C:\Documents and Settings\np0383\My Documents\C++\Test\TestDlg.cpp(181) : error C2664: 'FunWithFile' : cannot convert parameter 1 from 'class CFileDialog' to 'class CFileDialog' No copy constructor available for class 'CFileDialog' C:\Documents and Settings\np0383\My Documents\C++\Test\TestDlg.cpp(191) : error C2352: 'CWnd::MessageBoxA' : illegal call of non-static member function c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(2197) : see declaration of 'MessageBoxA' Error executing cl.exe. Test.exe - 2 error(s), 0 warning(s)
Any clues? Thanks in advance. Nick Parker
-
Ok, I am playing around with VC++ again :) and have only done a little work on a form and am already getting compiler errors X|. My guess is the error will be apparent to someone who uses VC++ often. This is what I have added:
void CTestDlg::OnShowFileDlg()
{CFileDialog cd(true); if(IDOK == cd.DoModal()) { FunWithFile(cd); }
}
void CTestDlg::FunWithFile(CFileDialog cd)
{MessageBox(\_T("Hello"), \_T("Test Message"), MB\_OK);
}
and I get the following error messages
--------------------Configuration: Test - Win32 Debug-------------------- Compiling... TestDlg.cpp C:\Documents and Settings\np0383\My Documents\C++\Test\TestDlg.cpp(181) : error C2664: 'FunWithFile' : cannot convert parameter 1 from 'class CFileDialog' to 'class CFileDialog' No copy constructor available for class 'CFileDialog' C:\Documents and Settings\np0383\My Documents\C++\Test\TestDlg.cpp(191) : error C2352: 'CWnd::MessageBoxA' : illegal call of non-static member function c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(2197) : see declaration of 'MessageBoxA' Error executing cl.exe. Test.exe - 2 error(s), 0 warning(s)
Any clues? Thanks in advance. Nick Parker
You are passing the entire dialog as an object and the compiler is trying to make a copy of the dialog. When you pass a parameter as an object and not a reference or pointer then the compiler will try to make a copy of your object. Bad practice. Instead pass a pointer to your dialog. Why are you passing a dialog? Anyway. Pass a pointer rather than the object. FunWithFile(&cd) and void FunWithFile( CFileDialog* cd ) or even better void FunWithFile( const CFileDialog* cd ) This way you pass a pointer but cannot change anything.
-
Ok, I am playing around with VC++ again :) and have only done a little work on a form and am already getting compiler errors X|. My guess is the error will be apparent to someone who uses VC++ often. This is what I have added:
void CTestDlg::OnShowFileDlg()
{CFileDialog cd(true); if(IDOK == cd.DoModal()) { FunWithFile(cd); }
}
void CTestDlg::FunWithFile(CFileDialog cd)
{MessageBox(\_T("Hello"), \_T("Test Message"), MB\_OK);
}
and I get the following error messages
--------------------Configuration: Test - Win32 Debug-------------------- Compiling... TestDlg.cpp C:\Documents and Settings\np0383\My Documents\C++\Test\TestDlg.cpp(181) : error C2664: 'FunWithFile' : cannot convert parameter 1 from 'class CFileDialog' to 'class CFileDialog' No copy constructor available for class 'CFileDialog' C:\Documents and Settings\np0383\My Documents\C++\Test\TestDlg.cpp(191) : error C2352: 'CWnd::MessageBoxA' : illegal call of non-static member function c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(2197) : see declaration of 'MessageBoxA' Error executing cl.exe. Test.exe - 2 error(s), 0 warning(s)
Any clues? Thanks in advance. Nick Parker
The clue has already been posted: you'd better pass a pointer rather than the whole object. You try to pass the whole object and consequently a copy of your object. To make a copy you need a constructor
CFileDialog(const CFileDialog &)
Even if you had it, it would be a poor way of implementing what you want because it would be too time- and resource- consuming. Thus pass a pointer and enjoy. Hope this helps. -
Ok, I am playing around with VC++ again :) and have only done a little work on a form and am already getting compiler errors X|. My guess is the error will be apparent to someone who uses VC++ often. This is what I have added:
void CTestDlg::OnShowFileDlg()
{CFileDialog cd(true); if(IDOK == cd.DoModal()) { FunWithFile(cd); }
}
void CTestDlg::FunWithFile(CFileDialog cd)
{MessageBox(\_T("Hello"), \_T("Test Message"), MB\_OK);
}
and I get the following error messages
--------------------Configuration: Test - Win32 Debug-------------------- Compiling... TestDlg.cpp C:\Documents and Settings\np0383\My Documents\C++\Test\TestDlg.cpp(181) : error C2664: 'FunWithFile' : cannot convert parameter 1 from 'class CFileDialog' to 'class CFileDialog' No copy constructor available for class 'CFileDialog' C:\Documents and Settings\np0383\My Documents\C++\Test\TestDlg.cpp(191) : error C2352: 'CWnd::MessageBoxA' : illegal call of non-static member function c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(2197) : see declaration of 'MessageBoxA' Error executing cl.exe. Test.exe - 2 error(s), 0 warning(s)
Any clues? Thanks in advance. Nick Parker
Pass the dialog by reference.
void CTestDlg::FunWithFile
(CFileDialog& cd)
{
MessageBox(_T("Hello"), _T("Test Message"), MB_OK);
}/ravi Let's put "civil" back in "civilization" http://www.ravib.com ravib@ravib.com
-
You are passing the entire dialog as an object and the compiler is trying to make a copy of the dialog. When you pass a parameter as an object and not a reference or pointer then the compiler will try to make a copy of your object. Bad practice. Instead pass a pointer to your dialog. Why are you passing a dialog? Anyway. Pass a pointer rather than the object. FunWithFile(&cd) and void FunWithFile( CFileDialog* cd ) or even better void FunWithFile( const CFileDialog* cd ) This way you pass a pointer but cannot change anything.
Thank you :) Nick Parker
-
The clue has already been posted: you'd better pass a pointer rather than the whole object. You try to pass the whole object and consequently a copy of your object. To make a copy you need a constructor
CFileDialog(const CFileDialog &)
Even if you had it, it would be a poor way of implementing what you want because it would be too time- and resource- consuming. Thus pass a pointer and enjoy. Hope this helps.Thank you :) Nick Parker
-
Pass the dialog by reference.
void CTestDlg::FunWithFile
(CFileDialog& cd)
{
MessageBox(_T("Hello"), _T("Test Message"), MB_OK);
}/ravi Let's put "civil" back in "civilization" http://www.ravib.com ravib@ravib.com
And another thank you :) Nick Parker
-
Pass the dialog by reference.
void CTestDlg::FunWithFile
(CFileDialog& cd)
{
MessageBox(_T("Hello"), _T("Test Message"), MB_OK);
}/ravi Let's put "civil" back in "civilization" http://www.ravib.com ravib@ravib.com
Ravi, The pointer thingy (like my naming convention) worked out great however I still get an error just with the MessageBox.
void CTestDlg::FunWithFile(CFileDialog& cd)
{
MessageBox(_T("Hello"), _T("Test Message"), MB_OK);
}Does this one make sense?
--------------------Configuration: Test - Win32 Debug-------------------- Compiling... TestDlg.cpp C:\Documents and Settings\np0383\My Documents\C++\Test\TestDlg.cpp(192) : error C2352: 'CWnd::MessageBoxA' : illegal call of non-static member function c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(2197) : see declaration of 'MessageBoxA' Error executing cl.exe. Test.exe - 1 error(s), 0 warning(s)
Also, how do you access the methods of the pointer to the object once it has been passed. I understand if you just want to say RTFM. :) Thanks again. Nick Parker
-
Ravi, The pointer thingy (like my naming convention) worked out great however I still get an error just with the MessageBox.
void CTestDlg::FunWithFile(CFileDialog& cd)
{
MessageBox(_T("Hello"), _T("Test Message"), MB_OK);
}Does this one make sense?
--------------------Configuration: Test - Win32 Debug-------------------- Compiling... TestDlg.cpp C:\Documents and Settings\np0383\My Documents\C++\Test\TestDlg.cpp(192) : error C2352: 'CWnd::MessageBoxA' : illegal call of non-static member function c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(2197) : see declaration of 'MessageBoxA' Error executing cl.exe. Test.exe - 1 error(s), 0 warning(s)
Also, how do you access the methods of the pointer to the object once it has been passed. I understand if you just want to say RTFM. :) Thanks again. Nick Parker
Hi Nick, You've probably got FunWithFile declared as a static member function, ie: in your header file, it probably looks like this:
class CTestDlg : public CDialog
{
...
static void FunWithFile(CFileDialog & cd);
...
};If that's the case, the reason you see this is because FunWithFile is a static member function, w/ explains the message "illegal call of non-static member function" (CWnd::MessageBox is not declared as static in it's header file). Here's a rule of thumb to follow w/ static class members (be they static data members or static member functions). The static keywoard (when used w/ classes) effectively makes the function or member data global, but limits it's scope to a class level. You can call CTestDlg::FunWithFile without an instance of a CTestDlg object because that function is effectively a global function, but you have to use the scope resolution operator to access the function from outside the class because the scope is limited to the class level.
void AnyFunctionYouWant()
{
CFileDialog fd(TRUE);
if( fd.DoModal() == IDOK )
{
CTestDlg::FunWithFile( fd );//uses the scope resolution operator
//to access the function....
}
}If you want to, you could change your test function to use
::MessageBox(NULL, _T("blah blah"), _T("blah"), MB_OK);
//or
AfxMessageBox(_T("blah blah blah");HTH, Wes Sonork ID 100.14017 wtheronjones
-
Hi Nick, You've probably got FunWithFile declared as a static member function, ie: in your header file, it probably looks like this:
class CTestDlg : public CDialog
{
...
static void FunWithFile(CFileDialog & cd);
...
};If that's the case, the reason you see this is because FunWithFile is a static member function, w/ explains the message "illegal call of non-static member function" (CWnd::MessageBox is not declared as static in it's header file). Here's a rule of thumb to follow w/ static class members (be they static data members or static member functions). The static keywoard (when used w/ classes) effectively makes the function or member data global, but limits it's scope to a class level. You can call CTestDlg::FunWithFile without an instance of a CTestDlg object because that function is effectively a global function, but you have to use the scope resolution operator to access the function from outside the class because the scope is limited to the class level.
void AnyFunctionYouWant()
{
CFileDialog fd(TRUE);
if( fd.DoModal() == IDOK )
{
CTestDlg::FunWithFile( fd );//uses the scope resolution operator
//to access the function....
}
}If you want to, you could change your test function to use
::MessageBox(NULL, _T("blah blah"), _T("blah"), MB_OK);
//or
AfxMessageBox(_T("blah blah blah");HTH, Wes Sonork ID 100.14017 wtheronjones
Thanks Wes, I'll try that as soon as I get back from the gym (just got home from work). :) Thanks again. Nick Parker
-
Ravi, The pointer thingy (like my naming convention) worked out great however I still get an error just with the MessageBox.
void CTestDlg::FunWithFile(CFileDialog& cd)
{
MessageBox(_T("Hello"), _T("Test Message"), MB_OK);
}Does this one make sense?
--------------------Configuration: Test - Win32 Debug-------------------- Compiling... TestDlg.cpp C:\Documents and Settings\np0383\My Documents\C++\Test\TestDlg.cpp(192) : error C2352: 'CWnd::MessageBoxA' : illegal call of non-static member function c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(2197) : see declaration of 'MessageBoxA' Error executing cl.exe. Test.exe - 1 error(s), 0 warning(s)
Also, how do you access the methods of the pointer to the object once it has been passed. I understand if you just want to say RTFM. :) Thanks again. Nick Parker
Nick, you need to use
::MessageBox()
to indicate global scope. Otherwise, the compiler looks for a method namedMessageBox()
that belongs toCTestDlg
. The reason why the error message refers to "MessageBoxA' instead of just "MessageBox" is because Windows #defines it so, since you're doing a non-Unicode build. /ravi Let's put "civil" back in "civilization" http://www.ravib.com ravib@ravib.com -
Ravi, The pointer thingy (like my naming convention) worked out great however I still get an error just with the MessageBox.
void CTestDlg::FunWithFile(CFileDialog& cd)
{
MessageBox(_T("Hello"), _T("Test Message"), MB_OK);
}Does this one make sense?
--------------------Configuration: Test - Win32 Debug-------------------- Compiling... TestDlg.cpp C:\Documents and Settings\np0383\My Documents\C++\Test\TestDlg.cpp(192) : error C2352: 'CWnd::MessageBoxA' : illegal call of non-static member function c:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(2197) : see declaration of 'MessageBoxA' Error executing cl.exe. Test.exe - 1 error(s), 0 warning(s)
Also, how do you access the methods of the pointer to the object once it has been passed. I understand if you just want to say RTFM. :) Thanks again. Nick Parker
Nick Parker wrote: Also, how do you access the methods of the pointer to the object once it has been passed. I understand if you just want to say RTFM. Thanks again. There are two ways to access methods through a pointer. For example, you have a class CFooClass with member function bar(). Normally, of course, we say:
CFooClass foo; foo.bar();
Now declare a pointer to CFooClass:CFooClass* pfoo = &foo; // pfoo is address of foo
Two methods of accessing member functions:// These two lines do the same thing (*pfoo).bar(); // Method 1 - use the dereference operator pfoo->bar(); // Method 2 - use the -> operator
I generally prefer method #2 is it's somewhat prettier to look at, but it really doesn't matter. Jeff Sand jsand at interaccess dot com -
Thanks Wes, I'll try that as soon as I get back from the gym (just got home from work). :) Thanks again. Nick Parker
That did it, thanks for the pointer (no pun intended). I'm sure I'll be back soon asking some other *dumb* question. :) Nick Parker
-
Nick Parker wrote: Also, how do you access the methods of the pointer to the object once it has been passed. I understand if you just want to say RTFM. Thanks again. There are two ways to access methods through a pointer. For example, you have a class CFooClass with member function bar(). Normally, of course, we say:
CFooClass foo; foo.bar();
Now declare a pointer to CFooClass:CFooClass* pfoo = &foo; // pfoo is address of foo
Two methods of accessing member functions:// These two lines do the same thing (*pfoo).bar(); // Method 1 - use the dereference operator pfoo->bar(); // Method 2 - use the -> operator
I generally prefer method #2 is it's somewhat prettier to look at, but it really doesn't matter. Jeff Sand jsand at interaccess dot comThanks Jeff, after looking at it I think that
pfoo->bar();
looks a lot better. Are there reasons for the two different methods where one only works in certain situations or are there just many ways of doing one thing? Thanks again. Nick Parker