Thread: constructor problem

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    20

    constructor problem

    As a part of an exercise I need to built Drawing class that contains an array
    of pointers to objects from other classes (different types of shapes)
    I have a few problems with the constructor.
    at first I tried:
    Code:
    typedef shape* pshape;
    ...
    
    Drawing(int n=10) :size_(n)
      {
    	drawing_ = new pshape[n];
    	for (int i=0; i<n; i++)
    	     drawing_[i] = new shape();
      }
    but after the line: drawing_ = new pshape[n]; the program returns to main()
    without entering the for loop;
    so I tried this instead:
    Code:
    Drawing(int n=10) :size_(n)
      {
    	pshape* temp;
           temp = new pshape[n];
           if (temp==NULL) exit(-1);
    	for (int i=0; i<n; i++)
            {
    	      temp = new shape();
                  if (temp[i]==NULL) exit(-1);
             }
           drawing_=temp;
      }
    and this time it returns to main right after pshape* temp;
    what wrong with the code?
    Last edited by stewie griffin; 01-17-2009 at 06:33 AM.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    20
    I think I solved it.
    I moved the implementation to the .C file and it worked.....

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Um? I think you are either misinterpreting what you are seeing in the debugger, or you have a problem elsewhere and shuffling code around simply moved the place where bad things start to happen to some other part of the code.

    If you don't understand why your changes fix things, chances are the problem is not fixed, just modified.

    In any case, I don't think that pointer typedefs like that make code any clearer (on the contrary), you should be using std::vector instead of dynamically allocated array, and shared_ptr (available in boost or compilers which support this C++0x addition) instead of naked pointers to single objects (or perhaps boost::ptr_vector).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    20
    Can't use STL.....

    this was my class declaration (Drawing.H):
    Code:
    class Drawing{
    private:
     shape** drawing_;
     int size_;
    public:
     Drawing(int n=10) :size_(n)
      {
    	drawing_ = new shape*[n];
    	for (int i=0; i<n; i++)
    	      drawing_[i] = new shape();
      }
    ...
    };
    when I changed it to:
    Code:
    class Drawing{
    private:
     shape** drawing_;
     int size_;
    public:
     Drawing(int n=10) ;
    ...
    };
    and added this to the Drawing.C file:
    Code:
    Drawing::Drawing(int n) :size_(n)
      {
    	drawing_ = new shape*[n];
    	for (int i=0; i<n; i++)
    	      drawing_[i] = new shape();
      }
    suddenly everything worked.....

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Perhaps it was a rebuild of the project that helped, or you still have a problem elsewhere which has now moved (for example because the constructor is no longer inlined), since changes like this don't fix real problems.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    BTW, if this is C++ code, shouldn't it be Drawing.cpp?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    20
    I'm working in Unix....

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That should not change the fact that .cpp is a valid extension (and perhaps the or one of the most famous extensions for C++ code files).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    That should not change the fact that .cpp is a valid extension
    Yes, but the point is that .C is a valid extension for a C++ source file, contrary to what cpjust implied. I would recommend .cpp over .C because of the reduced potential for confusion and that it will not cause problems even when the file names are case insensitive.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    Yes, but the point is that .C is a valid extension for a C++ source file, contrary to what cpjust implied. I would recommend .cpp over .C because of the reduced potential for confusion and that it will not cause problems even when the file names are case insensitive.
    True, true, but it doesn't change the fact that .cpp is a valid extension in Unix, as well, and can be used, if one would want to!
    Just because it's Unix doesn't mean .cpp is a non-valid extension, so the reply...
    Quote Originally Posted by stewie griffin View Post
    I'm working in Unix....
    ...was a bad one and had to be responded to ^_^
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    stewie griffin was saying that .C is a valid cpp extension in Unix, therefore it is fine for him to use it. I still recommend using .cpp, but whatever.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    It has been a long while since I used the compiler so I may be wrong, but didn't one of the Sun compilers require the extension to be '.C'?

    O_o

    Soma

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh my goodness... No...
    How moronic would that be...?
    Isn't .cpp de-facto standard for C++ source files?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    Isn't .cpp de-facto standard for C++ source files?
    No, it is not. I believe that .cc and .cxx are also quite popular.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Mmm, how about one of the de-facto standards? A popular one, to be sure, no?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed