Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. I'm stumped

I'm stumped

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharpvisual-studiocomdebugging
14 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nick Parker
    wrote on last edited by
    #1

    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


    R S R 3 Replies Last reply
    0
    • N 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


      R Offline
      R Offline
      Ranjan Banerji
      wrote on last edited by
      #2

      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.

      N 1 Reply Last reply
      0
      • N 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


        S Offline
        S Offline
        Space Ace
        wrote on last edited by
        #3

        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.

        N 1 Reply Last reply
        0
        • N 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


          R Offline
          R Offline
          Ravi Bhavnani
          wrote on last edited by
          #4

          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

          N 2 Replies Last reply
          0
          • R Ranjan Banerji

            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.

            N Offline
            N Offline
            Nick Parker
            wrote on last edited by
            #5

            Thank you :) Nick Parker


            1 Reply Last reply
            0
            • S Space Ace

              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.

              N Offline
              N Offline
              Nick Parker
              wrote on last edited by
              #6

              Thank you :) Nick Parker


              1 Reply Last reply
              0
              • R Ravi Bhavnani

                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

                N Offline
                N Offline
                Nick Parker
                wrote on last edited by
                #7

                And another thank you :) Nick Parker


                1 Reply Last reply
                0
                • R Ravi Bhavnani

                  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

                  N Offline
                  N Offline
                  Nick Parker
                  wrote on last edited by
                  #8

                  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


                  W R S 3 Replies Last reply
                  0
                  • N 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


                    W Offline
                    W Offline
                    Wes Jones
                    wrote on last edited by
                    #9

                    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

                    N 1 Reply Last reply
                    0
                    • W Wes Jones

                      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

                      N Offline
                      N Offline
                      Nick Parker
                      wrote on last edited by
                      #10

                      Thanks Wes, I'll try that as soon as I get back from the gym (just got home from work). :) Thanks again. Nick Parker


                      N 1 Reply Last reply
                      0
                      • N 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


                        R Offline
                        R Offline
                        Ravi Bhavnani
                        wrote on last edited by
                        #11

                        Nick, you need to use ::MessageBox() to indicate global scope. Otherwise, the compiler looks for a method named MessageBox() that belongs to CTestDlg. 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

                        1 Reply Last reply
                        0
                        • N 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


                          S Offline
                          S Offline
                          Shroom
                          wrote on last edited by
                          #12

                          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

                          N 1 Reply Last reply
                          0
                          • N Nick Parker

                            Thanks Wes, I'll try that as soon as I get back from the gym (just got home from work). :) Thanks again. Nick Parker


                            N Offline
                            N Offline
                            Nick Parker
                            wrote on last edited by
                            #13

                            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


                            1 Reply Last reply
                            0
                            • S Shroom

                              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

                              N Offline
                              N Offline
                              Nick Parker
                              wrote on last edited by
                              #14

                              Thanks 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


                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups