Making an array for a class

This is a discussion on Making an array for a class within the C++ Programming forums, part of the General Programming Boards category; I'm just learning C++, and I've been given a project to make a class Person with 6 different parts to ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    7

    Making an array for a class

    I'm just learning C++, and I've been given a project to make a class Person with 6 different parts to it. Then, I have to make an array of 5 people initializing each part for each person.

    I wrote the class, but I don't know how to initialize the array of people.

    Do I first have to declare:

    Person P1, P2, P3, P4, P5, P6; (with parameters)

    and then how do I declare the array?
    Do I have to write Person first again or do I make it a certain data type. And does the name of the array have to be Person or different from person?



    Thanks for any help

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    Code:
    class X
    {
    public:
        X(int, const char*){}
    };
    
    int main()
    {
        X x[6]={X(1,"A"), X(2,"B"), X(3,"C"), X(4,"D"), X(5,"E"), X(6,"F")};
    }
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    7
    Thanks for the help

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    7

    Arrow Does anyone know what's wrong with this?

    I'm having more problems. I made a class Person and each Person takes 6 parameters. Now, I'm supposed to make an array of 5 people. Can anyone tell me what is wrong with the code below? I really have not idea how to define this array, so I just tried to come up with something out of a book.

    Thanks for any help

    I attached my main file

    I'm getting errors like:

    left of '.Person' must have class/struct/union type
    syntax error : identifier 'Person'
    subscript requires array or pointer type
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    7
    Sorry, I don't know if anyone can read my attached file above. It's:


    Code:
    Person P1 = new Person ("1a", "1b", "1c", "1d", "1e", "1f");
        Person P2 = new Person ("2a", "2b", "2c", "2d", "2e", "2f");
        Person P3 = new Person ("3a", "3b", "3c", "3d", "3e", "3f");
        Person P4 = new Person ("4a", "4b", "4c", "4d", "4e", "4f");
        Person P5 = new Person ("5a", "5b", "5c", "5d", "5e", "5f");
     
        Person People[SIZE] = {P1, P2, P3, P4, P5};
    [CODE]

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Person P1 = new Person ("1a", "1b", "1c", "1d", "1e", "1f");

    the new operator will return an address that will be assigned to P1. Therefore P1 needs to be a pointer to type Person. The Person class also needs a non-default constructor that takes 6 strings in order for this to work.
    If P1 is a pointer to type Person, then the following is an error. People would need to be an array of pointers to type Person.
    Code:
    Person People[SIZE] = {P1, P2, P3, P4, P5};
    I think you will be better off not trying to initalize 5 Persons with six attributes, but to assign six attributes to 5 Persons.

    If you must initialize, try the template jinhoa provided:
    Code:
    //in Person class declare a constructor that describe the attributes
    //here each attribute is a string
    Person (string, string, string, string, string, string);
     
    //declare array of five Persons and initialize each Person
    Person People[5] = {Person("1a", "1b", "1c", "1d", "1e", "1f"),
    							Person("2a", "2b", etc.
    I usually use the assignment approach so I can't swear by the initialization syntax, though it would be easy enough to try if I had a compiler handy.
    Last edited by elad; 05-26-2005 at 03:54 PM.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to initialize a static pointer array in a class?
    By nadamson6 in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2005, 11:47 PM
  2. array of class object
    By TomButcher in forum C++ Programming
    Replies: 5
    Last Post: 09-03-2005, 10:48 AM
  3. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM
  5. Replies: 4
    Last Post: 09-12-2001, 03:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21