Question

I want to know why i can't use for loop in this method, im using it in my replace and insert method but in hear I get some error in for loop line.

 Contact return(const list<Contact> &listOf, int index) {
    for(list<Contact>::iterator it = listOf.begin(); it != listOf.end(); it++){

    }

    return Contact();   //dummy return. don't know what to do here
 }

I would appreciate some help on how to implement the code and what do I have to return?

I mean I know how to check with if statment to get the right object, but I dont know what do i have to do in if statment and what to write in return instead of "Contact();"

Was it helpful?

Solution

use a const_iterator for a const list

Contact return_function(const list<Contact> &listOf, int index) {
    for(list<Contact>::const_iterator it = listOf.begin(); it != listOf.end(); it++){

    }

    return Contact();   //dummy return
 }

Edit. fixed the function name as pointed by others...

OTHER TIPS

You need const_iterator, as listOf is const.

Contact return(const list<Contact> &listOf, int index) {
    //         ^^^^^---const container-----<
    //                                     ^
    // const_iterator--vvvvvv because of ->|
    for(list<Contact>::const_iterator it = listOf.begin(); it != listOf.end(); it++){

Also, you must rename your function, return is a keyword.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top