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. Using CArray From Dialog Box

Using CArray From Dialog Box

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
15 Posts 3 Posters 1 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.
  • J jw81

    Thanks Abyss, But I still dont get the idea. Let me explain the task I need the program to do. I have a coordinate display in CView (I use OnDraw to draw out the x-axis and y-axis) that allows user to use mouse pointer to pick out coordinate points on CView. Each points will be collected into an array (CArray). I made a struct that contents two elements x & y to store x and y coordinates. Next, I have a dialog to list out the points. The dialog can be accessed from the menu. I am new to VC++ but I have b/ground in C. How can use MFC to ease my coding? 1) Must I use struct so store x & y in CArray? Is there another way? 2) How can I use the CArray in my listing dialog? I tried to declare public variables in CView (CArray myarray) but it doesnt allow me to declare. Thanks a million.

    A Offline
    A Offline
    Abyss
    wrote on last edited by
    #5

    In my opinion David (see bellow) proposed a simpler approach so in the CView member function where you construct the dialog pass the array to you dialog constructor. I recommend to use CPoint instead of your structure which stores x,y. The member function of the CView can be called via MFC message command. 1) Use CPoint in CArray to store points. 2) It is not a good idea to create public members in any class.

    1 Reply Last reply
    0
    • D David Crow

      When constructing the dialog from within one of the view's methods, pass the address of the CArray object. Something like:

      CMyDialog dlg(&array);

      or

      CMyDialog dlg;
      dlg.m_pArray = &array;


      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

      J Offline
      J Offline
      jw81
      wrote on last edited by
      #6

      &array is the array from my view? It is from different class. Myarray is from CView however CDialog is a different class. I need to know how to use myarray in CDialog. THanks.

      D 1 Reply Last reply
      0
      • J jw81

        &array is the array from my view? It is from different class. Myarray is from CView however CDialog is a different class. I need to know how to use myarray in CDialog. THanks.

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #7

        Depending on how you want to pass the array from the view to the dialog, you'll either need to create a new constructor for the dialog, or make one of its member variables public so that the view can assign a value to it directly. In either case, the dialog will have a CArray* as a member variable. Once it has been assigned a value, it can be used as you like.


        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

        J 1 Reply Last reply
        0
        • D David Crow

          Depending on how you want to pass the array from the view to the dialog, you'll either need to create a new constructor for the dialog, or make one of its member variables public so that the view can assign a value to it directly. In either case, the dialog will have a CArray* as a member variable. Once it has been assigned a value, it can be used as you like.


          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          J Offline
          J Offline
          jw81
          wrote on last edited by
          #8

          I have a struct under CDoc struct MyStruct { float x; float y; }; I use it for CArray. I dont know how to define a public variable with struct. Thanks

          D 1 Reply Last reply
          0
          • J jw81

            I have a struct under CDoc struct MyStruct { float x; float y; }; I use it for CArray. I dont know how to define a public variable with struct. Thanks

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #9

            You need to put the struct in a .h file by itself. Then each of the .cpp or .h files that need it can simply #include it.

            mystruct.h:

            struct MyStruct
            {
            float x;
            float y;
            };

            typedef CArray<MyStruct*, MyStruct*> MyStructArray;

            Now the view and the dialog can each have a MyStructArray member variable.


            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            J 1 Reply Last reply
            0
            • D David Crow

              You need to put the struct in a .h file by itself. Then each of the .cpp or .h files that need it can simply #include it.

              mystruct.h:

              struct MyStruct
              {
              float x;
              float y;
              };

              typedef CArray<MyStruct*, MyStruct*> MyStructArray;

              Now the view and the dialog can each have a MyStructArray member variable.


              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

              J Offline
              J Offline
              jw81
              wrote on last edited by
              #10

              There are quite a number of errors: typedef CArray MyStructArray; error C2143: syntax error : missing ';' before '<' So, after I construct mystruct.h, I can add Add Member Variable (public) for Variable Type (MyStructArray)? Besides, I always get error on CDoc* GetDocument(); in my CView.h.

              D 1 Reply Last reply
              0
              • J jw81

                There are quite a number of errors: typedef CArray MyStructArray; error C2143: syntax error : missing ';' before '<' So, after I construct mystruct.h, I can add Add Member Variable (public) for Variable Type (MyStructArray)? Besides, I always get error on CDoc* GetDocument(); in my CView.h.

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #11

                jw81 wrote: typedef CArray MyStructArray; error C2143: syntax error : missing ';' before '<' CArray requires afxtempl.h to be included. I would suggest doing so in the project's stdafx.h file.


                "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                J 2 Replies Last reply
                0
                • D David Crow

                  jw81 wrote: typedef CArray MyStructArray; error C2143: syntax error : missing ';' before '<' CArray requires afxtempl.h to be included. I would suggest doing so in the project's stdafx.h file.


                  "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                  J Offline
                  J Offline
                  jw81
                  wrote on last edited by
                  #12

                  Hi The CDoc* GetDocument(); error is not solved yet. It only happens when I included my CView.h into the cpp. Thanks

                  D 1 Reply Last reply
                  0
                  • J jw81

                    Hi The CDoc* GetDocument(); error is not solved yet. It only happens when I included my CView.h into the cpp. Thanks

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #13

                    What file are you including CView.h in? I would hazard a guess that you also need to include the CDoc.h file right above that statement.


                    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                    J 1 Reply Last reply
                    0
                    • D David Crow

                      What file are you including CView.h in? I would hazard a guess that you also need to include the CDoc.h file right above that statement.


                      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                      J Offline
                      J Offline
                      jw81
                      wrote on last edited by
                      #14

                      CMainFrame for the menu

                      1 Reply Last reply
                      0
                      • D David Crow

                        jw81 wrote: typedef CArray MyStructArray; error C2143: syntax error : missing ';' before '<' CArray requires afxtempl.h to be included. I would suggest doing so in the project's stdafx.h file.


                        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                        J Offline
                        J Offline
                        jw81
                        wrote on last edited by
                        #15

                        Cant solve the CDoc error.

                        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