1

I have this data structure Seq which inherits the class vector but has some extra functionalities. Using this data structure Seq I have this predefined data structure:

typedef Seq< vector<int> > MxInt2d;

I want now to have a vector of several components of type MxInt2d; I was thinking about something like:

MxInt2d* loops;

it is just that I think I have to initialize this vector and I do not have a constructor for it. should I write a constructor in order to initialize it ?


So if on one hand I would have the declaration of the following data structure:

    MxInt2d myEdges_;

which is then initialized. And on the other hand the declaration of my variable loops:

 vector<MxInt2d> loops;

If I want to copy in loops[0] the first 5 elements of myEdges_, I would use the syntax:

for (int i=0;i<5;i++)
  loops[0].push_back(myEdges_[i]);

The program gets compiled but when I run it I obtain a bus error message.. The same stuff happens if I use the initialization for a second loop:

for (int i=0;i<5;i++){
    loops[1].push_back(myEdges_[i]);
 }

(sorry for my bad judgement, I am really new with vector) madalina

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
madalina
  • 377
  • 2
  • 7
  • 15

7 Answers7

3

Don't use pointers, unless you have to. Use vector again:

vector<MxInt2d> loops;

Right now, the loops container is empty (i.e. there are no matrices inside). If you want it to contain 2 MxInt2d object, you'll either have to insert them or initialize loops differently:

// loops will contain two empty MxInt2d objects
vector<MxInt2d> loops(2);

// after the following command,
// loops will contain 3 MxInt3d objects
loops.push_back(MxInt2d());

Only after you've populated loops you can start populating its elements.

avakar
  • 32,009
  • 9
  • 68
  • 103
3

Before getting into your problem-- an observation:

Are you inheriting the vector ? Deriving vector is not a good idea. All the the standard STL containers, lacks a virtual destructor, and publicly inheriting from classes without virtual destructors is a major C++ no-no.

aJ.
  • 34,624
  • 22
  • 86
  • 128
  • .. and the correct solution is to have a private vector member and manually delegate all calls to it. –  Apr 15 '09 at 12:36
  • That's an often cited point, but quite wrong IMO. It only matters if you want to delete a derived object via a base pointer. In most cases when deriving from an STL container, you want your derived class to be the base for a further class hierarchy (if any). Give that class a virtual dtor. –  Apr 16 '09 at 11:13
0

It's not a good idea to inherit from STL containers because they don't have virtual destructors, which can lead to underfined behaviour, if you try to delete a derived pointer through a base pointer (hope I got that right).

drby
  • 2,619
  • 25
  • 26
0

As it is, your data structure looks complex. By creating a pointer of it, you are making it even more complicated. It's better if you can create an array/vector of MxInt2d instead of making it a pointer.

Shree
  • 4,627
  • 6
  • 37
  • 49
0

No, not necessarily. std::vector will initialize all elements to 0, so if this is what you need, you won't have to write a custom constructor.

Dimitri C.
  • 21,861
  • 21
  • 85
  • 101
0

Arrays are evil in C++. You can use vector for almost anything that you can use array for. One common problem; that of initialization can be solved. Refer to this discussion how-to-initialize std::vector like 'C' array.

So, you can use 'vector loops;' without problems.

HTH,

Community
  • 1
  • 1
Abhay
  • 7,092
  • 3
  • 36
  • 50
0

You have a couple of problems here.

One, you should not derive from vector. If you need code that does custom stuff with a vector, you should write a class with your custom stuff that has-a vector, not is-a vector. For example:

class Seq
{
public:
   // assorted constructors

// construct vec_ with zero elements
   Seq() {}; 

// construct vec_ with one element
   Seq(int singleItemToAdd) 
: vec_(1, singleItemToAdd) {};

// construct vec_ with 'multipleItemsToAdd' elements
   Seq(const int* multipleItemsToAdd, size_t numItemsToAdd) 
: vec_(multipleItemsToAdd,multipleItemsToAdd+numItemsToAdd) {}; 


   // assorted custom operations (instead of deriving from vector)
   void customSeqOperation() const
   {
      /// your custom stuff here
      :   :
   }
private:
   vector<int> vec_;
};

Next problem, you are saying this code compiles but crashes at runtime:

vector<MxInt2d> loops;
for (int i=0;i<5;i++)
  loops[0].push_back(myEdges_[i]);

If this is your essentially compete code, the reason why it crashes is because there is no element at loops[0] -- you haven't added it yet. You need to add an element to loops (which is a vector) before you can access the first one:

vector<MxInt2d> loops;
MxInt2d firstElement = getTheElement();
loops.push_back(firstElement);   
for (int i=0;i<5;i++)
  loops[0].push_back(myEdges_[i]);
John Dibling
  • 99,718
  • 31
  • 186
  • 324