#### Archived

This topic is now archived and is closed to further replies.

# Pointers and std::list

This topic is 5216 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, I recently tried to do something with std::list but I got a problem.. I'm trying to do this: --edit-- see my second post for the correct exemple The "iter==ptr" gives me this error: "error C2679: binary '==' : no operator defined which takes a right-hand operand of type 'class std::list >::iterator' (or the re is no acceptable conversion)" What is wrong? When I'm doing the exact same thing with a vector, it works fine.. :/ How could I make this work? Thanks [edited by - Hedos on May 8, 2004 12:52:06 AM] [edited by - Hedos on May 9, 2004 8:25:39 PM]

##### Share on other sites
What is wrong?
A list iterator is not a pointer.

When I''m doing the exact same thing with a vector, it works fine.. :/
Pure accident. Implementation-specific. Don''t rely on it.

How could I make this work?
As it is currently, it cannot work. The object placed in the list is a copy of MyObj, not MyObj itself (I assume that''s what you meant). It is therefore located at a different address.

If MyClass supports it (or if you use a type for which that operation is legal) you can, however, test for object equality.

if (*iter==MyObj)

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

##### Share on other sites
oh, damn, my exemple was wrong, sorry...

This is the correct exemple I should have given:
MyClass MyObj;MyClass *ptr;std::list<MyClass> MyList;MyList.push_back(MyObj);std::list<MyClass>::iterator iter;iter=MyList.begin();ptr = iter;if(iter==ptr){  //Do something}

Well, this would work fine with vectors and as I far as I know, iterators are kinda like pointers.. so there must be a wait to do it with list iterators too?

##### Share on other sites
You''ve made a /copy/ of MyClass''s in the list, so you cannot test for equality by using the identity (address of/pointer to).

##### Share on other sites
Hum.. there must be a way to have the address of the element in the list that my iterator is referencing to?
That's the only thing I need to do

quote:
Original post by TheSalmon

Gives me an "Access Violation." :/

[edited by - Hedos on May 9, 2004 2:08:37 PM]

##### Share on other sites
Up..

No one know if it''s possible to get the address of an element "referenced" in a std::list iterator?

##### Share on other sites
i have always done it in the way described above as well and never had a problem. eg.

std::list::iterator it = aList.begin();

MyClass* myclass = &(*it);

1. 1
2. 2
JoeJ
20
3. 3
frob
18
4. 4
5. 5

• 10
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632204
• Total Posts
3004755

×
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.20035140216350555, "perplexity": 3757.971865742446}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 20, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2018-34/segments/1534221218357.92/warc/CC-MAIN-20180821151743-20180821171743-00204.warc.gz"}