Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > return value of Iterator in a list??

Reply
Thread Tools

return value of Iterator in a list??

 
 
cylin
Guest
Posts: n/a
 
      07-29-2003
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;
}
----------------------------------------------------------------------------
------------------


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-29-2003

"cylin" <(E-Mail Removed)> wrote in message
news:bg539i$kequg$(E-Mail Removed)-berlin.de...
> 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


 
Reply With Quote
 
 
 
 
ES Kim
Guest
Posts: n/a
 
      07-29-2003
"cylin" <(E-Mail Removed)> wrote in message
news:bg539i$kequg$(E-Mail Removed)-berlin.de...
> 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.
But why not list<test*> in the first place?

> delete c;
> delete d;
> cin.get();
> return 0;
> }
> ----------------------------------------------------------------------------
> ------------------


--
ES Kim


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments