Connecting Tech Pros Worldwide Help | Site Map

Help understanding pointers to classes within containers...

  #1  
Old July 23rd, 2005, 01:50 AM
deancoo
Guest
 
Posts: 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;
}




  #2  
Old July 23rd, 2005, 01:50 AM
Rolf Magnus
Guest
 
Posts: n/a

re: Help understanding pointers to classes within containers...


deancoo wrote:
[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? Thanks
> for any help.[/color]

Doesn't the compiler provide any hint? No error messages?
[color=blue]
> 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[/color]

j is an iterator into a vector of pointers, so (*j) is a pointer. You have
to dereference that pointer:

(*j)->int_value = 0;

Btw: Are you sure you need to store pointers in your container and that you
need to wrap int?
[color=blue]
> };
> /* */
>
> system("PAUSE");
> return EXIT_SUCCESS;
> }[/color]

  #3  
Old July 23rd, 2005, 01:51 AM
deancoo
Guest
 
Posts: n/a

re: Help understanding pointers to classes within containers...


Yup, that was it. Thank you Rolf. The compiler hint was:

"request for member `int_value' in `
(&j)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator*() const
"

I haven't worked with templates a whole lot so I'm not able to interpret the
above message very well. Are there any tips anyone might offer for working
with containers and interpreting compiler messages? Thanks. Again.

d

"Rolf Magnus" <ramagnus@t-online.de> wrote in message
news:cungmf$ojb$05$1@news.t-online.com...[color=blue]
> deancoo wrote:
>[color=green]
>> 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.[/color]
>
> Doesn't the compiler provide any hint? No error messages?
>[color=green]
>> 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[/color]
>
> j is an iterator into a vector of pointers, so (*j) is a pointer. You have
> to dereference that pointer:
>
> (*j)->int_value = 0;
>
> Btw: Are you sure you need to store pointers in your container and that
> you
> need to wrap int?
>[color=green]
>> };
>> /* */
>>
>> system("PAUSE");
>> return EXIT_SUCCESS;
>> }[/color]
>[/color]


  #4  
Old July 23rd, 2005, 01:51 AM
Rolf Magnus
Guest
 
Posts: n/a

re: Help understanding pointers to classes within containers...


deancoo wrote:
[color=blue]
> Yup, that was it. Thank you Rolf. The compiler hint was:
>
> "request for member `int_value' in `
> (&j)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator*()
> const "[/color]

The compiler says that what the iterator's operator* returns is not
something that has a member int_value, i.e. you cannot do
(*the_iterator).int_value. Btw, I guess this is not the whole message.
Usually, g++ also tells you what the actual type is.
[color=blue]
> I haven't worked with templates a whole lot so I'm not able to interpret
> the above message very well.[/color]

Fair enough. But if you have code that the compiler doesn't accept and you
want to post it here, it's always a good idea to include the error
messages. Even if you don't know what the messages mean, someone else here
might.
[color=blue]
> Are there any tips anyone might offer for working with containers and
> interpreting compiler messages? Thanks. Again.[/color]

Well, much of the standard library is heavily template based, and compiler
error messages do tend to get a bit cryptic in connection with templates,
so it is sometimes hard to find out what is meant. But after you have seen
some, you will get a feel for it.
Closed Thread