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! ] 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! ]
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! ]
"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! ]
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! ]
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! ]
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! ]
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! ]
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! ]
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! ] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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>...
|
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...
|
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...
|
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...
| |
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>?...
|
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,...
|
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...
|
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);
|
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);
...
|
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,...
| |
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...
|
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,...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |