public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* STL Question not answered in FAQ
@ 1999-03-19 10:43 Gordon Neal
       [not found] ` < 001b01be7237$e0298de0$5acb6689@pdxnw392.pdx.intel.com >
  1999-03-31 23:46 ` Gordon Neal
  0 siblings, 2 replies; 4+ messages in thread
From: Gordon Neal @ 1999-03-19 10:43 UTC (permalink / raw)
  To: egcs

I am trying to use STL in the latest egcs release and have encountered a
problem that seems fairly simple but I have yest to figure out why my code
doesn't work. Here is an example of the code:
#include <stdio.h>
#include "stl.h"
class PtrWrapper {
  int *a;
public:
  PtrWrapper() {}
  PtrWrapper(int *x):a(x) {}
  ~PtrWrapper() {}
  PtrWrapper(const PtrWrapper& x) {}
  PtrWrapper& operator= (const PtrWrapper& x) { }
};
class B {
  friend class D;
  list<PtrWrapper> ptr;
public:
  B() {}
  ~B() {}
  B(const B& x) {}
  B& operator= (const B& x) { }
  addPtr(int *a) {
    ptr.push_back(PtrWrapper(a));
  }
};
class C {
  friend class D;
  vector<B> listb;
public:
  C() {}
  C(int size) {
    int i;
    listb.reserve(size);
    printf("Reserved %d for list B\n",size);
    for (i = 0; i < size; i++) {
      listb.push_back(B());
    }
  }
  ~C() {}
  C(const C& x) {}
  C& operator= (const C& x) { }
};
class D {
  vector<C> listc;
  int i;
  int j;
public:
  D() {}
  D(int sizec, int sizeb) {
    int i;
    listc.reserve(sizec);
    for (i = 0; i < sizec; i++) {
      listc.push_back(C(sizeb));
    }
  }
  ~D() {
  }
  D(const D& x) {}
  D& operator= (const D& x) { }
  print() {
    int k = 0, l = 0;
    vector<C>::iterator i;
    vector<B>::iterator j;
    printf("The size of vector c is %d\n",listc.size());
    for (i = listc.begin(); i != listc.end(); i++) {
      printf("The size of vector b for listc[%d] is
%d\n",k++,i->listb.size());
 	for (j = i->listb.begin(); j != i->listb.end() ; j++) {
        printf("j = %d\n",l++);
      }
    }
  }
};
main(int argc, char**argv) {
    D d(10,20);
    d.print();
}
The output of this:
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
The size of vector c is 10
The size of vector b for listc[0] is 0
The size of vector b for listc[1] is 0
The size of vector b for listc[2] is 0
The size of vector b for listc[3] is 0
The size of vector b for listc[4] is 0
The size of vector b for listc[5] is 0
The size of vector b for listc[6] is 0
The size of vector b for listc[7] is 0
The size of vector b for listc[8] is 0
The size of vector b for listc[9] is 0

The question is why is listb size 0? What am I doing wrong?

Thanks

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: STL Question not answered in FAQ
       [not found] ` < 001b01be7237$e0298de0$5acb6689@pdxnw392.pdx.intel.com >
@ 1999-03-19 11:16   ` Joe Buck
  1999-03-31 23:46     ` Joe Buck
  0 siblings, 1 reply; 4+ messages in thread
From: Joe Buck @ 1999-03-19 11:16 UTC (permalink / raw)
  To: Gordon Neal; +Cc: egcs

> I am trying to use STL in the latest egcs release and have encountered a
> problem that seems fairly simple but I have yest to figure out why my code
> doesn't work.

It's because you defined a broken copy constructor in class C:

> class C {
>   friend class D;
>   vector<B> listb;
> public:
>   C() {}
>   C(int size) {
>     int i;
>     listb.reserve(size);
>     printf("Reserved %d for list B\n",size);
>     for (i = 0; i < size; i++) {
>       listb.push_back(B());
>     }
>   }
>   ~C() {}
>   C(const C& x) {}  // HERE.
>   C& operator= (const C& x) { }
> };

Then when you say

>       listc.push_back(C(sizeb));

The copy constructor is used to create the C objects in listc.
The resulting C object will have an empty vector.

DO NOT attempt to use the STL to store objects that do not meet the
requirements.  At minimum you need a copy constructor that does a
correct copy; for some algorithms you also need an assignment operator
that does a correct assignment (here your assignment operator is
also broken).

^ permalink raw reply	[flat|nested] 4+ messages in thread

* STL Question not answered in FAQ
  1999-03-19 10:43 STL Question not answered in FAQ Gordon Neal
       [not found] ` < 001b01be7237$e0298de0$5acb6689@pdxnw392.pdx.intel.com >
@ 1999-03-31 23:46 ` Gordon Neal
  1 sibling, 0 replies; 4+ messages in thread
From: Gordon Neal @ 1999-03-31 23:46 UTC (permalink / raw)
  To: egcs

I am trying to use STL in the latest egcs release and have encountered a
problem that seems fairly simple but I have yest to figure out why my code
doesn't work. Here is an example of the code:
#include <stdio.h>
#include "stl.h"
class PtrWrapper {
  int *a;
public:
  PtrWrapper() {}
  PtrWrapper(int *x):a(x) {}
  ~PtrWrapper() {}
  PtrWrapper(const PtrWrapper& x) {}
  PtrWrapper& operator= (const PtrWrapper& x) { }
};
class B {
  friend class D;
  list<PtrWrapper> ptr;
public:
  B() {}
  ~B() {}
  B(const B& x) {}
  B& operator= (const B& x) { }
  addPtr(int *a) {
    ptr.push_back(PtrWrapper(a));
  }
};
class C {
  friend class D;
  vector<B> listb;
public:
  C() {}
  C(int size) {
    int i;
    listb.reserve(size);
    printf("Reserved %d for list B\n",size);
    for (i = 0; i < size; i++) {
      listb.push_back(B());
    }
  }
  ~C() {}
  C(const C& x) {}
  C& operator= (const C& x) { }
};
class D {
  vector<C> listc;
  int i;
  int j;
public:
  D() {}
  D(int sizec, int sizeb) {
    int i;
    listc.reserve(sizec);
    for (i = 0; i < sizec; i++) {
      listc.push_back(C(sizeb));
    }
  }
  ~D() {
  }
  D(const D& x) {}
  D& operator= (const D& x) { }
  print() {
    int k = 0, l = 0;
    vector<C>::iterator i;
    vector<B>::iterator j;
    printf("The size of vector c is %d\n",listc.size());
    for (i = listc.begin(); i != listc.end(); i++) {
      printf("The size of vector b for listc[%d] is
%d\n",k++,i->listb.size());
 	for (j = i->listb.begin(); j != i->listb.end() ; j++) {
        printf("j = %d\n",l++);
      }
    }
  }
};
main(int argc, char**argv) {
    D d(10,20);
    d.print();
}
The output of this:
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
Reserved 20 for list B
The size of vector c is 10
The size of vector b for listc[0] is 0
The size of vector b for listc[1] is 0
The size of vector b for listc[2] is 0
The size of vector b for listc[3] is 0
The size of vector b for listc[4] is 0
The size of vector b for listc[5] is 0
The size of vector b for listc[6] is 0
The size of vector b for listc[7] is 0
The size of vector b for listc[8] is 0
The size of vector b for listc[9] is 0

The question is why is listb size 0? What am I doing wrong?

Thanks


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: STL Question not answered in FAQ
  1999-03-19 11:16   ` Joe Buck
@ 1999-03-31 23:46     ` Joe Buck
  0 siblings, 0 replies; 4+ messages in thread
From: Joe Buck @ 1999-03-31 23:46 UTC (permalink / raw)
  To: Gordon Neal; +Cc: egcs

> I am trying to use STL in the latest egcs release and have encountered a
> problem that seems fairly simple but I have yest to figure out why my code
> doesn't work.

It's because you defined a broken copy constructor in class C:

> class C {
>   friend class D;
>   vector<B> listb;
> public:
>   C() {}
>   C(int size) {
>     int i;
>     listb.reserve(size);
>     printf("Reserved %d for list B\n",size);
>     for (i = 0; i < size; i++) {
>       listb.push_back(B());
>     }
>   }
>   ~C() {}
>   C(const C& x) {}  // HERE.
>   C& operator= (const C& x) { }
> };

Then when you say

>       listc.push_back(C(sizeb));

The copy constructor is used to create the C objects in listc.
The resulting C object will have an empty vector.

DO NOT attempt to use the STL to store objects that do not meet the
requirements.  At minimum you need a copy constructor that does a
correct copy; for some algorithms you also need an assignment operator
that does a correct assignment (here your assignment operator is
also broken).

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1999-03-31 23:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-03-19 10:43 STL Question not answered in FAQ Gordon Neal
     [not found] ` < 001b01be7237$e0298de0$5acb6689@pdxnw392.pdx.intel.com >
1999-03-19 11:16   ` Joe Buck
1999-03-31 23:46     ` Joe Buck
1999-03-31 23:46 ` Gordon Neal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).