473,394 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

return value of Iterator in a list??

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;
}
----------------------------------------------------------------------------
------------------
Jul 19 '05 #1
2 8410

"cylin" <cy***@avant.com.tw> wrote in message
news:bg************@ID-154203.news.uni-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
Jul 19 '05 #2
"cylin" <cy***@avant.com.tw> wrote in message
news:bg************@ID-154203.news.uni-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
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Huibuh | last post by:
I start hating RETURN!!! list<happy> //#include <list>// is searched via iterator (ptr) so long until the argument "number" of the class "happy" is equal now. The function "findhappy" works...
5
by: Bob | last post by:
Hi, I have a std::vector, say myVec, of some user defined object, say myOb. In my code, I have a function that searches myVec for a particular myOb. The way I was doing this was searching...
6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
15
by: Andrew Brampton | last post by:
Hi, This may sound a odd question, but I wanted to know how you return a list of data from a function. These are some of the ways I know how, and I was wondering which method you normally use....
7
by: andreas | last post by:
Hello, I have a problem with iterators in a fairly complex polygonal mesh data structure which is implemented using lists of geometric entities. However, the problem in itself is fairly simple:...
4
by: Christian Christmann | last post by:
Hi, here is my small program: Header file: Class myClass { public: void function();
6
by: TOMERDR | last post by:
Hi,i am new to stl and i have a question regarding iterators I have class day contains a map of appointments: map<time_t,Appointment> m_Appointments; I would like to write a function...
15
by: ajos | last post by:
Hello all, Im making a dependent drop down for my application for filtering purpose. I have populated the drop down from the database and retrieved in the html. <tr> <td valign="top"...
12
by: subramanian100in | last post by:
Below is my understanding about count algorithms. Return type of count and count_if algorithms is iterator_traits<InputIterator>::difference_type. If the container contains more than...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.