By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

Help understanding pointers to classes within containers...

P: n/a
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! ]

Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"deancoo" <s2*******@yahoo.ca> writes:
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?

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! ]
Jul 23 '05 #2

P: n/a
Hi,

deancoo wrote:
for (j=my_containers.begin(); j!=my_containers.end(); j++) {
(*j).int_value = 0;
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;
};


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! ]
Jul 23 '05 #3

P: n/a
"deancoo" <s2*******@yahoo.ca> wrote in message
news:PjGPd.28533$K54.22987@edtnps84...
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? 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);
}; 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) );
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

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! ]
Jul 23 '05 #4

P: n/a
deancoo wrote:

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


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
kb******@gascad.at

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

P: n/a
In article <PjGPd.28533$K54.22987@edtnps84>, deancoo
<s2*******@yahoo.ca> writes
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; j will be an iterator whose values are int_container*'s.
for (j=my_containers.begin(); j!=my_containers.end(); j++) {
(*j).int_value = 0; // this is the line the compiler points to as

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! ]
Jul 23 '05 #6

P: n/a
deancoo wrote:
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. [snip] class int_container {
public:
int int_value;
}; [snip] vector<int_container*> my_containers; [snip] 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


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*************@comcast.net

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

P: n/a
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! ]
Jul 23 '05 #8

P: n/a
deancoo wrote:
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;
}


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! ]
Jul 23 '05 #9

P: n/a
deancoo wrote:
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;
}

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! ]
Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.