Help understanding pointers to classes within containers...

D

deancoo

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;
}
 
R

Rolf Magnus

deancoo said:
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.

Doesn't the compiler provide any hint? No error messages?
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

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?
 
D

deancoo

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

"request for member `int_value' in `
(&j)->__gnu_cxx::__normal_iterator<_Iterator, _Container>::eek:perator*() 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 said:
deancoo said:
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.

Doesn't the compiler provide any hint? No error messages?
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

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?
};
/* */

system("PAUSE");
return EXIT_SUCCESS;
}
 
R

Rolf Magnus

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

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

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.
I haven't worked with templates a whole lot so I'm not able to interpret
the above message very well.

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.
Are there any tips anyone might offer for working with containers and
interpreting compiler messages? Thanks. Again.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top