return value of Iterator in a list??

C

cylin

Dear all,

How do I change the codes in for-loop to print the value(m_dblXMax) in a
list?
Please help, thanks.
----------------------------------------------------------------------------
------------------
#include <iostream>
#include <list>
using namespace std;

typedef list<void*> List;
typedef List::iterator ListIterator;

class test {
public:
test():m_dblXMax(100) {}
double m_dblXMax;
};

int main()
{

List mylist;
test *c=new test;
c->m_dblXMax=1000;
mylist.push_back(c);
test *d=new test;
d->m_dblXMax=1001;
mylist.push_back(d);

for (ListIterator i=mylist.begin();i!=mylist.end();i++) {
// Got a error left of '->m_dblXMax' must point to class/struct/union
cout << (*i)->m_dblXMax << endl;
}
delete c;
delete d;
cin.get();
return 0;
}
 
J

John Harrison

cylin said:
Dear all,

How do I change the codes in for-loop to print the value(m_dblXMax) in a
list?
Please help, thanks.
-------------------------------------------------------------------------- --
------------------
#include <iostream>
#include <list>
using namespace std;

typedef list<void*> List;
typedef List::iterator ListIterator;

class test {
public:
test():m_dblXMax(100) {}
double m_dblXMax;
};

int main()
{

List mylist;
test *c=new test;
c->m_dblXMax=1000;
mylist.push_back(c);
test *d=new test;
d->m_dblXMax=1001;
mylist.push_back(d);

for (ListIterator i=mylist.begin();i!=mylist.end();i++) {
// Got a error left of '->m_dblXMax' must point to class/struct/union
cout << (*i)->m_dblXMax << endl;
}
delete c;
delete d;
cin.get();
return 0;
}

Is there any good reason for this mess of pointers? The sensible code would
be

class test {
public:
test():m_dblXMax(100) {}
test(double x):m_dblXMax(x) {}
double m_dblXMax;
};

typedef list<test> List;
typedef List::iterator ListIterator;

int main()
{

List mylist;
mylist.push_back(test(1000));
mylist.push_back(test(1001));

for (ListIterator i=mylist.begin();i!=mylist.end();++i)
{
cout << i->m_dblXMax << endl;
}
cin.get();
return 0;
}

If you really need to mess about with void pointers (I really doubt it) then
you'll need to add a cast.

cout << static_cast<test*>(*i)->m_dblXMax << endl;

john
 
E

ES Kim

cylin said:
Dear all,

How do I change the codes in for-loop to print the value(m_dblXMax) in a
list?
Please help, thanks.
----------------------------------------------------------------------------
------------------
#include <iostream>
#include <list>
using namespace std;

typedef list<void*> List;
typedef List::iterator ListIterator;

class test {
public:
test():m_dblXMax(100) {}
double m_dblXMax;
};

int main()
{

List mylist;
test *c=new test;
c->m_dblXMax=1000;
mylist.push_back(c);
test *d=new test;
d->m_dblXMax=1001;
mylist.push_back(d);

for (ListIterator i=mylist.begin();i!=mylist.end();i++) {
// Got a error left of '->m_dblXMax' must point to class/struct/union
cout << (*i)->m_dblXMax << endl;
}

There is no implicit conversion from void* to test*.
Try static_cast<test*>(*i)->m_dblXMax.
 

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,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top