Sets and Maps, function wont compile.

Hello,

I have to write code to create this function.

/*
set<Book> publishedBefore(int year);
returns a set of Books that were published before a given year.
*/


My private fields for my BookDataBase class are

private:
map<string, Book> bookData;
map<string, Book>::iterator iter;



and my code for trying to solve the problem is


1
2
3
4
5
6
7
8
9
10
11
12
13
14
set<Book> BookDataBase::publishedBefore(int year){

    set<Book> myBook;

    for(iter=bookData.begin(); iter!=bookData.end(); iter++){

        Book tempBook = iter->second;
        
        if(tempBook.getYearOfPublication() < year){
            myBook.insert(iter->second);
        }
    }
    return myBook;
}


Whenever I try to compile this, Code Blocks opens up a file called "stl_functions.h" with the error

"error: no match for 'operator<' (operand types are 'const Book' and 'const Book')"

What am I doing wrong?

Thank You
Last edited on
To put objects into a std::set, they have to be sortable. The sorting is done using the class < operator. Your Book class doesn't have a < operator.
Thank you Repeater, I was able to complete my assignment. Cheers
Topic archived. No new replies allowed.