Connecting Tech Pros Worldwide Forums | Help | Site Map

Help understanding pointers to classes within containers...

deancoo
Guest
 
Posts: n/a
#1: Jul 23 '05
Hello all,

I'm new to the C++ STL, and am trying to wrap my head around why the
following piece of code doesn't compile. What am I doing wrong? Thanks for
any help.

d

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

class int_container {
public:
int int_value;
};

int main(int argc, char *argv[])
{
vector<int_container*> my_containers;

for (int i=1; i<=5; i++) {
my_containers.push_back(new int_container);
};

vector<int_container*>::iterator j;

for (j=my_containers.begin(); j!=my_containers.end(); j++) {
(*j).int_value = 0; // this is the line the compiler points to as
culprit
};
/* */

system("PAUSE");
return EXIT_SUCCESS;
}



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Tim Love
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Help understanding pointers to classes within containers...


"deancoo" <s2cuts555@yahoo.ca> writes:
[color=blue]
>Hello all,[/color]
[color=blue]
>I'm new to the C++ STL, and am trying to wrap my head around why the
>following piece of code doesn't compile. What am I doing wrong?[/color]
Your iterator points to an element which is in turn a pointer, so you
need
(*j)->int_value = 0;
rather than
(*j).int_value = 0;

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Maciej Sobczak
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Help understanding pointers to classes within containers...


Hi,

deancoo wrote:
[color=blue]
> for (j=my_containers.begin(); j!=my_containers.end(); j++) {
> (*j).int_value = 0;[/color]

better:
(*j)->int_value = 0;

or:
(**j).int_value = 0;

or:
int_container *p = *j;
p->int_value = 0;

or:
int_container *p = *j;
(*p).int_value = 0;
[color=blue]
> };[/color]

The problem is that j is an iterator into the container of pointers.
If you do *j then you get one of the pointers stored in the container.
You have tried to do . on that pointer, which is not good.
You can, however, do -> on the pointer or dereference it further, as
shown above.

You may also consider storing boost::shared_ptr instead of plain
pointers in the container - this will help you manage the dynamic memory
allocated by each operator new.

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Ivan Vecerina
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Help understanding pointers to classes within containers...


"deancoo" <s2cuts555@yahoo.ca> wrote in message
news:PjGPd.28533$K54.22987@edtnps84...[color=blue]
> Hello all,
>
> I'm new to the C++ STL, and am trying to wrap my head around why the
> following piece of code doesn't compile. What am I doing wrong?[/color]
[color=blue]
> class int_container {
> public:
> int int_value;
> };
>
> int main(int argc, char *argv[])
> {
> vector<int_container*> my_containers;
>
> for (int i=1; i<=5; i++) {
> my_containers.push_back(new int_container);
> };[/color]
There is no point here in using a vector of *pointers*.
Much easier instead:
vector<int_container> my_containes;
...
my_containers.push_back( int_container() );

Even better: add a constructor to int_container:
class int_container {
public:
int int_value;

int_container( int initialValue )
: int_value(initialValue) {}
};
Then you can specify the value of each item as you add it:
my_containers.push_back( int_container(i) );
[color=blue]
> vector<int_container*>::iterator j;
>
> for (j=my_containers.begin(); j!=my_containers.end(); j++) {
> (*j).int_value = 0; // this is the line the compiler points to as[/color]
This will be correct with the change above.
With the original code, 'j' is an iterator to a pointer,
so you needed to dereference it twice: (**j).int_value = 0;



hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Karl Heinz Buchegger
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Help understanding pointers to classes within containers...


deancoo wrote:[color=blue]
>
> Hello all,
>
> I'm new to the C++ STL, and am trying to wrap my head around why the
> following piece of code doesn't compile. What am I doing wrong? Thanks for
> any help.
>
> d
>
> #include <cstdlib>
> #include <iostream>
> #include <vector>
>
> using namespace std;
>
> class int_container {
> public:
> int int_value;
> };
>
> int main(int argc, char *argv[])
> {
> vector<int_container*> my_containers;
>
> for (int i=1; i<=5; i++) {
> my_containers.push_back(new int_container);
> };
>
> vector<int_container*>::iterator j;
>
> for (j=my_containers.begin(); j!=my_containers.end(); j++) {
> (*j).int_value = 0; // this is the line the compiler points to as[/color]

j is the iterator.
*j (dereferencing the iterator) brings you to the thing that is
stored inside the container, which is a int_container*.
Thus *j evaluates to a pointer.

(*j)->int_value = 0;


--
Karl Heinz Buchegger
kbuchegg@gascad.at

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Francis Glassborow
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Help understanding pointers to classes within containers...


In article <PjGPd.28533$K54.22987@edtnps84>, deancoo
<s2cuts555@yahoo.ca> writes[color=blue]
>int main(int argc, char *argv[])
>{
> vector<int_container*> my_containers;
>
> for (int i=1; i<=5; i++) {
> my_containers.push_back(new int_container);
> };
>
> vector<int_container*>::iterator j;[/color]
j will be an iterator whose values are int_container*'s.[color=blue]
>
> for (j=my_containers.begin(); j!=my_containers.end(); j++) {
> (*j).int_value = 0; // this is the line the compiler points to as[/color]
So that line needs to be:
(*j)->int_value = 0;

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Antoun Kanawati
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Help understanding pointers to classes within containers...


deancoo wrote:[color=blue]
> Hello all,
>
> I'm new to the C++ STL, and am trying to wrap my head around why the
> following piece of code doesn't compile. What am I doing wrong? Thanks for
> any help.[/color]
[snip][color=blue]
> class int_container {
> public:
> int int_value;
> };[/color]
[snip][color=blue]
> vector<int_container*> my_containers;[/color]
[snip][color=blue]
> vector<int_container*>::iterator j;
>
> for (j=my_containers.begin(); j!=my_containers.end(); j++) {
> (*j).int_value = 0; // this is the line the compiler points to as[/color]

The iterator looks like a pointer, and the elements are also pointers.
So, you need two levels of indirections, try:

(*j)->int_value

--
A. Kanawati
NO.antounk.SPAM@comcast.net

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Joel
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Help understanding pointers to classes within containers...


j is an iterator, so *j is an int_container*, not an int_container.

I think what you really want to say is

(*j)->int_value = 0;

Also, in general, you don't want to store pointers in containers,
because you can't use any algorithm to remove pointers from a
container. See Scott Meyer's STL book for more on this, and a
convenient solution using smart pointers.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
gipsy boy
Guest
 
Posts: n/a
#9: Jul 23 '05

re: Help understanding pointers to classes within containers...


deancoo wrote:[color=blue]
> Hello all,
>
> I'm new to the C++ STL, and am trying to wrap my head around why the
> following piece of code doesn't compile. What am I doing wrong? Thanks for
> any help.
>
> d
>
> #include <cstdlib>
> #include <iostream>
> #include <vector>
>
> using namespace std;
>
> class int_container {
> public:
> int int_value;
> };
>
> int main(int argc, char *argv[])
> {
> vector<int_container*> my_containers;
>
> for (int i=1; i<=5; i++) {
> my_containers.push_back(new int_container);
> };
>
> vector<int_container*>::iterator j;
>
> for (j=my_containers.begin(); j!=my_containers.end(); j++) {
> (*j).int_value = 0; // this is the line the compiler points to as
> culprit
> };
> /* */
>
> system("PAUSE");
> return EXIT_SUCCESS;
> }[/color]

don't you mean

(*j)->int_value = 0; ? (or rather (*j)->setIntval(0); :) )
Since (*j) is of type int_container*, you need to directly access
members with the -> operator.

Don't forget to destruct the vector btw..

--
- gipsy boy

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Mark Van Peteghem
Guest
 
Posts: n/a
#10: Jul 23 '05

re: Help understanding pointers to classes within containers...


deancoo wrote:
[color=blue]
>Hello all,
>
>I'm new to the C++ STL, and am trying to wrap my head around why the
>following piece of code doesn't compile. What am I doing wrong? Thanks for
>any help.
>
>d
>
>#include <cstdlib>
>#include <iostream>
>#include <vector>
>
>using namespace std;
>
>class int_container {
>public:
> int int_value;
>};
>
>int main(int argc, char *argv[])
>{
> vector<int_container*> my_containers;
>
> for (int i=1; i<=5; i++) {
> my_containers.push_back(new int_container);
> };
>
> vector<int_container*>::iterator j;
>
> for (j=my_containers.begin(); j!=my_containers.end(); j++) {
> (*j).int_value = 0; // this is the line the compiler points to as
>culprit
> };
>/* */
>
> system("PAUSE");
> return EXIT_SUCCESS;
>}
>
>[/color]
Change the line to (*j)->int_value = 0;
Explanation: *j is a reference to an element of the container,
and therefore it is of type int_container*, not int_container.

--
Mark dot Van dot Peteghem at q-mentum dot com
http://www.q-mentum.com -- easier and more powerful unit testing

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Closed Thread