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_cont ainer*> my_containers;
for (int i=1; i<=5; i++) {
my_containers.p ush_back(new int_container);
};
vector<int_cont ainer*>::iterat or j;
for (j=my_container s.begin(); j!=my_container s.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++.m oderated. First time posters: Do this! ] 9 1948
"deancoo" <s2*******@yaho o.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++.m oderated. First time posters: Do this! ]
Hi,
deancoo wrote: for (j=my_container s.begin(); j!=my_container s.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_p tr 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++.m oderated. First time posters: Do this! ]
"deancoo" <s2*******@yaho o.ca> wrote in message
news:PjGPd.2853 3$K54.22987@edt nps84... 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_cont ainer*> my_containers;
for (int i=1; i<=5; i++) { my_containers.p ush_back(new int_container); };
There is no point here in using a vector of *pointers*.
Much easier instead:
vector<int_cont ainer> my_containes;
...
my_containers.p ush_back( int_container() );
Even better: add a constructor to int_container:
class int_container {
public:
int int_value;
int_container( int initialValue )
: int_value(initi alValue) {}
};
Then you can specify the value of each item as you add it:
my_containers.p ush_back( int_container(i ) );
vector<int_cont ainer*>::iterat or j;
for (j=my_container s.begin(); j!=my_container s.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++.m oderated. 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_cont ainer*> my_containers;
for (int i=1; i<=5; i++) { my_containers.p ush_back(new int_container); };
vector<int_cont ainer*>::iterat or j;
for (j=my_container s.begin(); j!=my_container s.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++.m oderated. First time posters: Do this! ]
In article <PjGPd.28533$K5 4.22987@edtnps8 4>, deancoo
<s2*******@yaho o.ca> writes int main(int argc, char *argv[]) { vector<int_cont ainer*> my_containers;
for (int i=1; i<=5; i++) { my_containers.p ush_back(new int_container); };
vector<int_cont ainer*>::iterat or j;
j will be an iterator whose values are int_container*' s. for (j=my_container s.begin(); j!=my_container s.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++.m oderated. 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_cont ainer*> my_containers;
[snip] vector<int_cont ainer*>::iterat or j;
for (j=my_container s.begin(); j!=my_container s.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++.m oderated. 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++.m oderated. 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_cont ainer*> my_containers;
for (int i=1; i<=5; i++) { my_containers.p ush_back(new int_container); };
vector<int_cont ainer*>::iterat or j;
for (j=my_container s.begin(); j!=my_container s.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++.m oderated. 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_cont ainer*> my_containers;
for (int i=1; i<=5; i++) { my_containers.p ush_back(new int_container); };
vector<int_cont ainer*>::iterat or j;
for (j=my_container s.begin(); j!=my_container s.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++.m oderated. 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 POD, because it has std::string and
other classes requiring proper destruction, although all drill down to POD
and strings eventually).
Classic code layout:
|
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>
#include <iostream>
|
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 frequently, I thought
it'd be a better idea to manage additional containers which are
initialized with pointers to the objects in the primary containers and
sort those (only pointers have to be copied around then).
However, that also means if I...
|
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 Fred : public BedrockCitizen { ... };
class Wilma : public BedrockCitizen { ... };
class Barney : public BedrockCitizen { ... };
|
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 inserted/deleted at the end and at beginning
and the std::list can have data inserted/deleted anywhere.
But how can those differences be physically understood?
| |
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>?
To be specific, here is my situation, I have 2 classes A and B. Class A
contains a private vector of pointers to B objects and I need to sort this
|
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, but I just don't know.
I don't like them because I don't trust them. I use new and delete on pure
pointers instead.
Do you 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
********************************************************
For this teeny job, please refer to:
http://feeds.reddit.com/feed/8fu/?o=25
|
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);
for(int i = 0; i < num; i++)
{
int* my_int_ptr = new int;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |