Thread: Declaring an array of classes...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    17

    Question Declaring an array of classes...

    It seems that these bits of code don't work:
    Code:
        CP wPawn('W')[8]; //expected a semi-colon
    
        CP wPawn[8]('W'); //bad array initializer
    The class:
    Code:
    class CP
        {
           public:
                  CP(char color);             
                  ~CP();
                  bool isWhite();
           private:
                   char symbol;
                   bool white;     
           friend class square;
        };
    Should I just create a default constructor and declare the color for each individual object through a member function? Apparantly its impossible to define an array like that... If Anyone could tell me why, I would like to know.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    an array of classes is just like an array of ints, ie: int myInts[8];
    that creates 8 ints, each seperate from the other. to access or set the value they contain you must do something like: myInts[2] = 3;

    try and apply this to your situation

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You cannot specify a constructor argument when declaring an array of class objects unless you use an array initializer and initialize each one individually. You should use a vector, which is usually a better option than an array anyway. IMO it is definitely better than adding a default constructor when the class design doesn't call for one.
    Last edited by Daved; 10-26-2006 at 06:31 PM. Reason: Correction from Prelude

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    17
    nadroj: yes, I know how to do that, read the whole topic before you post, not just the title. Thanks anyway.

    Daved: Ok, I've never used vecotrs before, but I'll look it up, thanks.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    yes, I know how to do that, read the whole topic before you post, not just the title. Thanks anyway.
    i did read the whole thread so dont assume i didnt, thanks.

    what you want to do is create an array of 8 objects of [whatever] type and call the constructor for each at the same time, correct?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You cannot specify a constructor argument when declaring an array of class objects.
    Array initializer?
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You cannot specify a constructor argument when declaring an array of class objects unless you use an array initializer.

    But look up vector anyway, it is a good option.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    17
    Solution:
    Code:
    vector <CP> wPawn('W'); //
    This declares a vector (essentially a resizeable array) of the CP class using a constructor with parameters. It seems to dodge the previous syntax errors associated the normal array. However, I did have to declare an empty CP default constructor for it to work. Thanks, Daved.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    To make a vector of size 8 all initialized with 'W' you would use this syntax:
    Code:
    vector <CP> wPawn(8, 'W');

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    17
    Ooooooh, I wish I looked at that yesterday...

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Daved
    You cannot specify a constructor argument when declaring an array of class objects unless you use an array initializer and initialize each one individually. You should use a vector, which is usually a better option than an array anyway. IMO it is definitely better than adding a default constructor when the class design doesn't call for one.
    Isn't constructor called automatically?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It is. What's your point?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Stack Overflow when declaring array
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2009, 05:00 AM
  3. An Array of Classes in a different Class
    By mas0nite in forum C++ Programming
    Replies: 4
    Last Post: 10-05-2006, 02:28 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Array of classes within class definition
    By subdene in forum C++ Programming
    Replies: 2
    Last Post: 02-21-2005, 05:57 PM