473,511 Members | 14,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help understanding pointers to classes within containers...

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
9 1927
"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
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
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2186
by: JackC | last post by:
Hi Problem: I wish to use a pimpl to hide implementation/storage of a class (T), but I also want to hold objects of that class (T) in an std::vector<T> (or similar). T is non trivial (i.e. not...
3
310
by: deancoo | last post by:
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>...
18
3022
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
12
1964
by: ex_ottoyuhr | last post by:
I have a situation more or less as follows, and it's causing me no end of trouble; I'd appreciate anyone's advice on the matter... Given these classes: class BedrockCitizen { ... }; class...
18
2178
by: Simon | last post by:
Hi, I understand what one the differences between std::vector, std::deque and std::list is, the std::vector can have data inserted/deleted at the end. The std::deque can have data...
9
1672
by: Timothee Groleau | last post by:
Hi all, My name is Tim, I'm just getting started with C++ and this is my first post to the group. Is there a standard recommended approach to use a vector of pointers along with <algorithm>?...
92
5001
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
0
5519
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
5
2099
by: Renato | last post by:
I have an array of pointers to class Shape. I create 4 items and display their values. shapes Shape *shapes; shapes = new Shape (p1); shapes = new Triangle (p1); shapes = new Polygon (p2);
13
2859
by: smp | last post by:
Does anyone know why making a vector of pointers is so much less efficient than a vector of objects? For a simple example: int num = 20; vector<int*v_int_ptr; v_int_ptr.reserve(num); ...
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.