Thread: for loop to create an array of objects

  1. #1
    Shadow12345
    Guest

    for loop to create an array of objects

    I am trying to create a for loop that creates an array of objects. I have a base class(god) and a derived class(people).

    Code:
    -----------------------------------------------------------------------------
    ...
    cout << "\nMake new people\n";
    cout << "\nEnter the number of people you want created\n";
    cin >> NumberOfPeople;
    ******* god* Array[NumberOfPeople];
    for(x = 0; x < NumberOfPeople; x++)
    {
    Array[x] = new person;
    };
    ...
    -------------------------------------------------------------------------------
    I have three errors on the line with the asterisks
    they are:
    1) error C2057: expected constant expression
    2) error C2466: cannot allocate an array of constant size 0
    3) error C2133: 'Array' : unknown size
    I compiled under Visual Studio, I don't think posting the rest of the code is necessary.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You have to define the array before using it. It must contain a constant number of elements. You can't choose the number of elements at runtime, try a std::vector.

  3. #3
    Shadow12345
    Guest
    The array does have a constant number of elemnents, in this case it is NumberOfPeople, and what is std::vector? I have never heard of that before.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Sorry, it needs a 'compile-time' number of elements. You're asking for the number of elements at runtime; the user could enter any number, so it's not constant.

    >and what is std::vector?

    Get a decent introductory C++ text. Do a search for "Bruce Eckel" on something like google.

  5. #5
    Shadow12345
    Guest
    O.K sounds good to me. What do you think is a decent book on Visual C++? I am going to buy one tomorrow so I have been asking around.

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    What do you want to 'make' with Visual C++.

  7. #7
    Shadow12345
    Guest
    well right now I am interested in making windows applications, but I am also going to buy a book on OpenGL.

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    For Windows apps try a Charles Petzold book. You could do a search for "The Red Book" for OpenGl, I believe it's freely available (there are probably better books available, I'm not really up to date on OpenGl documentation). Both of these will presume that you have an decent understanding of at least C.

  9. #9
    Shadow12345
    Guest

    Bragging rights

    Ok I looked into the std::vector thingamabob like you sugggested and OH MY GOD IT WORKS (so far)
    I am very happy and I just wanted to show you this code:

    code:
    ----------------------------------------------------------------------------------
    #include <iostream>
    #include <string.h>
    #include <vector>
    using namespace std;

    class god
    {
    public:
    virtual void speak() { cout << "\nGod is speaking!\n";}

    god::god() { cout << "\nGod constructor\n";}


    void setname(char *name) {strncpy(itsname, name, 50);}

    char *getname() {return itsname;}


    protected:
    char itsname[50];

    };


    class person: public god
    {
    public:
    void speak() {cout << "\nThe person is speaking!\n";}
    };



    int main()
    {
    int x;
    int y = 0;

    typedef std::vector<god*> GODVECTOR;
    GODVECTOR MyVector;
    cout << "\nHow many gods do you want produced?\n";
    cin >> x;

    do
    {
    MyVector.push_back(new god);
    y++;
    }
    while(y < x);

    return 0;
    }
    ----------------------------------------------------------------------------------
    Thanks a billion for helping me!

  10. #10
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Since you're using vectors, you may aswell take the leap and use std::strings instead of char*.

    Also, if you're concerned about efficiency (as some C programmer may point out if I don't) you could look up std::vector::reserve to allocate the memory obtained by the 'x' input before calling push_back each time. That way you ensure an allocation isn't performed each time you add an element.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Objects + Array
    By Cpro in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2007, 11:14 PM
  2. Trying to create a 5 element integer array
    By nadeni0119 in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2003, 08:35 PM
  3. Create Array size Question
    By popohoma in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2002, 03:04 AM
  4. Create array of CArray
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 09-30-2002, 11:22 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM