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. how to use an Array of a class? (modified)

how to use an Array of a class? (modified)

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
13 Posts 5 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.
  • M Mark Salsbery

    The last time you posted this question, someone (I believe it was PJ Arends) asked what the value of 'i' is when it crashes. Umm, what is that value? You have an array of 10 pointers. You only show the first item in the array being used. Something is happening elsewhere or code you haven't shown. What line of code does the "crash" occur? Mark

    "If you can dodge a wrench, you can dodge a ball."

    M Offline
    M Offline
    MohammadAmiry
    wrote on last edited by
    #4

    At any value of i (e.g. i=0) it crashes on initialization of the element (before Save() function)

    1 Reply Last reply
    0
    • D David Crow

      MohammadAmiry wrote:

      Clips[i]=new CClipSaver;

      Shouldn't this be:

      Clips[i] = new CClipSaver();

      MohammadAmiry wrote:

      Clips[i]->Save();. But it crashes!

      Have you stepped into the Step() method to figure out why? If, however, Clips[i] is NULL, you should not be calling the Save() method.


      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

      "Judge not by the eye but by the heart." - Native American Proverb

      M Offline
      M Offline
      MohammadAmiry
      wrote on last edited by
      #5

      Oh yes!

      Clips[i] = new CClipSaver();

      is correct and it was a missprint (although I have tried both)! It throws an Access Violation error!

      M 1 Reply Last reply
      0
      • D David Crow

        MohammadAmiry wrote:

        Clips[i]=new CClipSaver;

        Shouldn't this be:

        Clips[i] = new CClipSaver();

        MohammadAmiry wrote:

        Clips[i]->Save();. But it crashes!

        Have you stepped into the Step() method to figure out why? If, however, Clips[i] is NULL, you should not be calling the Save() method.


        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

        "Judge not by the eye but by the heart." - Native American Proverb

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #6

        Hey DavidCrow, I noticed that too. What's the difference between those? I always "thought" that without parenthesis, the default constructor gets called. I always use the parenthesis so I never bothered to look it up :) Thanks! Mark

        "If you can dodge a wrench, you can dodge a ball."

        D 1 Reply Last reply
        0
        • M MohammadAmiry

          Oh yes!

          Clips[i] = new CClipSaver();

          is correct and it was a missprint (although I have tried both)! It throws an Access Violation error!

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #7

          MohammadAmiry wrote:

          It throws an Access Violation error!

          Where????? Throw us a bone here :)

          "If you can dodge a wrench, you can dodge a ball."

          1 Reply Last reply
          0
          • M MohammadAmiry

            Hi I have a class named CClipSaver and I want to create an array of this class. I do this as follows:

            CClipSaver* Clips[10];
            .
            int i=0;
            Clips[i]=new CClipSaver;
            Clips[i]->Save();
            .
            

            But it crashes! Note: When I do not use array (i.e. CClipSaver * Clip; Clip=new ClipSaver(); Clip**->**Save(); ) everything works fine. How should I declare and use the array?

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #8

            I think the best advice people can give you is: use your debugger, it will save you hours of hassle. If you never used it, it's time you start to ;). Put some breakpoints (by pressing F9) in your program where you want to check relevant information and start the debugger (press F5). If you handle it well, it will give you much more information than we could give you (because we just have a very short code snippet).


            Cédric Moonen Software developer
            Charting control [v1.1]

            L 1 Reply Last reply
            0
            • M Mark Salsbery

              Hey DavidCrow, I noticed that too. What's the difference between those? I always "thought" that without parenthesis, the default constructor gets called. I always use the parenthesis so I never bothered to look it up :) Thanks! Mark

              "If you can dodge a wrench, you can dodge a ball."

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

              Mark Salsbery wrote:

              What's the difference between those?

              For non-integral types, there is none. For integral types (e.g., int, char), it amounts to whether initialization occurs or not.

              Mark Salsbery wrote:

              I always use the parenthesis so I never bothered to look it up

              Same here.


              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

              "Judge not by the eye but by the heart." - Native American Proverb

              M 1 Reply Last reply
              0
              • D David Crow

                Mark Salsbery wrote:

                What's the difference between those?

                For non-integral types, there is none. For integral types (e.g., int, char), it amounts to whether initialization occurs or not.

                Mark Salsbery wrote:

                I always use the parenthesis so I never bothered to look it up

                Same here.


                "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                "Judge not by the eye but by the heart." - Native American Proverb

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #10

                Cool. Thanks for saving me the trouble of looking it up myself :)

                "If you can dodge a wrench, you can dodge a ball."

                1 Reply Last reply
                0
                • M MohammadAmiry

                  Hi I have a class named CClipSaver and I want to create an array of this class. I do this as follows:

                  CClipSaver* Clips[10];
                  .
                  int i=0;
                  Clips[i]=new CClipSaver;
                  Clips[i]->Save();
                  .
                  

                  But it crashes! Note: When I do not use array (i.e. CClipSaver * Clip; Clip=new ClipSaver(); Clip**->**Save(); ) everything works fine. How should I declare and use the array?

                  M Offline
                  M Offline
                  MohammadAmiry
                  wrote on last edited by
                  #11

                  In fact I use this code inside an MFC dll. When I put F9 on the line and press F5 to load the file that uses the dll, it disables the breakpoint expressing that the breakpoint is disabled because it is at the beginning of the program!! I now used it in an MFC exe, surprisingly, NO ERROR occurs!! :confused::^)

                  1 Reply Last reply
                  0
                  • C Cedric Moonen

                    I think the best advice people can give you is: use your debugger, it will save you hours of hassle. If you never used it, it's time you start to ;). Put some breakpoints (by pressing F9) in your program where you want to check relevant information and start the debugger (press F5). If you handle it well, it will give you much more information than we could give you (because we just have a very short code snippet).


                    Cédric Moonen Software developer
                    Charting control [v1.1]

                    L Offline
                    L Offline
                    led mike
                    wrote on last edited by
                    #12

                    Cedric Moonen wrote:

                    use your debugger, it will save you hours of hassle.

                    :beer: :jig: Because with the following definition of CClipSaver the posted code works just fine

                    class CClipSaver
                    {
                    public:
                    void Save(){ cout << "Clip.Save" << endl;}
                    };

                    led mike

                    1 Reply Last reply
                    0
                    • M MohammadAmiry

                      Hi I have a class named CClipSaver and I want to create an array of this class. I do this as follows:

                      CClipSaver* Clips[10];
                      .
                      int i=0;
                      Clips[i]=new CClipSaver;
                      Clips[i]->Save();
                      .
                      

                      But it crashes! Note: When I do not use array (i.e. CClipSaver * Clip; Clip=new ClipSaver(); Clip**->**Save(); ) everything works fine. How should I declare and use the array?

                      M Offline
                      M Offline
                      MohammadAmiry
                      wrote on last edited by
                      #13

                      OH I finally figured out!!! The dll had to be called from a vb6 application (the hell with that!) X| In declarations, there was a missing ByVal statement, which caused address value of i to be sent instead of its value. Thanks all you folks for your attention which helped me figure this out, by checking other possibilities! :-D :rose:

                      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