0

I need to create in c++ an array of 20 objects, because i will need to manipulate them all in order to draw them in OpenGL.

I've tried:

Animal::Cat *_listCats[20];

Then i need to inicialize them with coordinates. I have a constructor and a Seter

Cat(float _x, float _y) :
 _xOffSet(_x), _yOffSet(_y) {}


void CatCoordinates(float x, float y){
_xOffSet = x;
_yOffSet = y;
    }

And I've a method that draw in OpenGL a Cat

void DrawCat() 
{
...do things...
 }

Cat is a specific class in the namespace Animal and i call from another file. The problem is that i'm having sigsevs and I don't know how to create 20 cats and draw them efficiently with cicles.

2
  • 2
    That's an array of 20 Cat pointers, not an array of 20 Cat objects. Commented Mar 6, 2013 at 10:16
  • So, should I remove * ? Commented Mar 6, 2013 at 10:17

3 Answers 3

1

If you want an array of 20 objects, you can use an std::array:

#include <array>

std::array<Animal::Cat, 20> listCats_; // leading _ reserved for implementation

If you don't have C++11 support, you can use the TR1 version:

#include <tr1/array>

std::tr1::array<Animal::Cat, 20> listCats_;
Sign up to request clarification or add additional context in comments.

Comments

1

That array is just an array of pointers. It has 20 pointers to Animal::Cat. It doesn't give you any Animal::Cat objects to point to and the pointers are left not pointing anywhere. To get them to point to actual Animal::Cat objects, you would need to do new Animal::Cat() for each pointer element and assign them:

for (int i = 0; i < 20; i++) {
  _listCats[i] = new Animal::Cat();
}

The new expression dynamically allocates an Animal::Cat object and gives you a pointer to it, which you can then assign to the pointers in your array.

However, you're much better off not using dynamically allocated memory at all. Just change it to:

Animal::Cat _listCats[20];

This gives you an array of 20 Animal::Cats - genuine objects that you can start using right away.

Alternatively, you can be even safer by using containers from the C++ Standard Library. An std::array<Animal::Cat, 20> would be purrfect here.

Comments

0

Is there specific need for exactly 20 members? Your can use normal vector to store your object, and let it grow as needed. I don't see need for exact number.

Also, under some compilers you would miss std::array, so vector might be fine. Advantage is, it will be easier to read each cat data from file or based on some config file.

    #include <vector>

    std::vector<Animal::Cat *> cats;

    void createCats()
    {
      using namespace Animal;

      Cat *cat1 = new Cat(1, 2);
      // load image or some data maybe?
      cats.push_back(cat1);

      Cat *cat2 = new Cat(3,4);
      cats.push_back(cat2);

      // unless data loading is needed.
      cats.push_back(new Cat(5,6));
      cats.push_back(new Cat(7,8));
    }

    void DrawAllCats()
    {
      for (unsigned i=0; i< cats.size(); i++) {
        cats[i]->DrawCat();
      }
    }

    // because cats are allocated dynamically, they will not be destroyed on vector destructor.
    // use delete or smart pointers (std::auto_ptr)
    void destroyCats()
    {
      for (unsigned i=0; i<cats.size(); i++) {
        delete cats[i];
        cats[i] = NULL;
      }
    }

I suggest you study a bit way of C++ objects, it differs from languages like Java. There are references only when you specify one, objects can be copied, passed by reference or pointed at by pointer.

You can use for example use std::vector cats. But you should define constructor without parameters. See http://www.cplusplus.com/doc/tutorial/pointers/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.