Thread: How to call the constructor in this case

  1. #1
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    How to call the constructor in this case

    Hi there!
    I´m in doubt(of course)! How can I call the constructor of objects that are in an array within a constructor of another class ?
    Note: the array isn´t of pointers!
    I´m sending my two files for better explanation
    Thanks for any help!

    Note: I´m getting this erros when trying to compile:
    no matching function for call to `Piece::Piece ()'
    Nothing more to tell about me...
    Happy day =)

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    If you have an array of objects that need constructing, then that object must have a default constructor. If a default constructor doesn't work in your case - then use an array of pointers and allocate each element.

    gg

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I didn't look at your code, but I think I have a pretty good idea of what you're asking.
    If you have an array of objects that need constructing, then that object must have a default constructor. If a default constructor doesn't work in your case - then use an array of pointers and allocate each element.
    I don't think this is true, consider the following:

    Code:
    class Piece
    {
    public:
        Piece(int id) : m_id(id) {};
        ~Piece() {};
    
    protected:
        int m_id;
    };
    
    ...
    
    Piece Pieces[5] = { Piece(0), Piece(1), Piece(2), Piece(3), Piece(4) };

  4. #4
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    First: thanks!
    Second: I thought about using pointers, but it would be a extreme wast of time for me, since I would have to deallocate and reallocate objects all the time, or change pointers... I prefer to switch among the types of pieces... is not possible that this can´t be done!
    Nothing more to tell about me...
    Happy day =)

  5. #5
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Originally posted by Eibro
    I didn't look at your code, but I think I have a pretty good idea of what you're asking.I don't think this is true, consider the following:

    Code:
    class Piece
    {
    public:
        Piece(int id) : m_id(id) {};
        ~Piece() {};
    
    protected:
        int m_id;
    };
    
    ...
    
    Piece Pieces[5] = { Piece(0), Piece(1), Piece(2), Piece(3), Piece(4) };
    But I have a 8x8 matrix! This means calling the constructor 64 times!
    Nothing more to tell about me...
    Happy day =)

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I come back to the boards and get schooled by Eibro - with a cool new avatar too!

    gustavosserra, it is possible, use static initialization like Eibro showed.

    gg

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Originally posted by gustavosserra
    But I have a 8x8 matrix! This means calling the constructor 64 times!
    Yep, its gotta happen somehow. Either the compiler will generate the code for you (if you have a default constructor) or you will have to do it yourself with static initialization or a for loop and pointers.

    gg

  8. #8
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    *sigh* this is so sad
    But thanks you all! I will use pointers...
    Nothing more to tell about me...
    Happy day =)

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    can't you use a vector or something?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  2. Intel syntax on MinGW ?
    By TmX in forum Tech Board
    Replies: 2
    Last Post: 01-06-2007, 09:44 AM
  3. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM