"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! ]