stl vector help

G

Greg

if I have:


vector<Car*> car;

vector<Car*>::iterator cari;
for(cari = car.begin(); cari != car.end(); cari++)
{
*(cari)->printDetails();
}

where printDetails() is a function in class Car why can I not comple this
code the error?

I get is:
main.cpp: In function `int main()':
main.cpp:75: error: base operand of `->' has non-pointer type `Car'
 
A

Alf P. Steinbach

* Greg:
if I have:


vector<Car*> car;

vector<Car*>::iterator cari;
for(cari = car.begin(); cari != car.end(); cari++)
{
*(cari)->printDetails();
}

where printDetails() is a function in class Car why can I not comple this
code the error?

Possibly because Car doesn't define operator->() ... ;-)

Try

*(cari).printDetails();

or

cari->printDetails();

but not both.
 
A

Alf P. Steinbach

* Alf P. Steinbach:
* Greg:

Possibly because Car doesn't define operator->() ... ;-)

Try

*(cari).printDetails();

Typo (my fingers aped what my eyes saw, I have little control over all these
semi-autonomous subsystems),

(*cari).printDetails();
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Alf said:
* Greg:



Possibly because Car doesn't define operator->() ... ;-)

Try

*(cari).printDetails();

or

cari->printDetails();

but not both.

Huh?
I would say:

(*cari)->printDetails();

Since the vector holds Car*.
Or am I missing something?


Stefan
 
J

John Harrison

Greg said:
if I have:


vector<Car*> car;

vector<Car*>::iterator cari;
for(cari = car.begin(); cari != car.end(); cari++)
{
*(cari)->printDetails();
}

where printDetails() is a function in class Car why can I not comple this
code the error?

(*cari)->printDetails();

john
 
R

red floyd

Alf said:
* Alf P. Steinbach:



Typo (my fingers aped what my eyes saw, I have little control over all these
semi-autonomous subsystems),

(*cari).printDetails();


Alf, it's a vector of Car*, so it should be

(*cari)->printDetails();
 
A

Arndt Muehlenfeld

Alf said:
* Alf P. Steinbach: ....
Typo (my fingers aped what my eyes saw, I have little control over all
these semi-autonomous subsystems),

(*cari).printDetails();

Of course both, as the element-type of vector is a pointer.
But write
(*cari)->printDetails();
 
Y

Yefim Keselman

Greg said:
if I have:


vector<Car*> car;

vector<Car*>::iterator cari;
for(cari = car.begin(); cari != car.end(); cari++)
{
*(cari)->printDetails();
}

where printDetails() is a function in class Car why can I not comple this
code the error?

I get is:
main.cpp: In function `int main()':
main.cpp:75: error: base operand of `->' has non-pointer type `Car'
It might be easier to avoid this kind of errors if you did it in steps:
vector<Car*>::iterator cari;
for (cari = car.begin(); cari != car.end(); ++cari)
{
Car* carp = *cari;
carp->printDetails();
}
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top