Below is the class used as the value in a map:
class Book
{
int m_nId;
public:
// Book() { } <----- Why is this required?
Book( int id ): m_nId( id ) { }
};
Inside main():
map< int, Book > mapBooks;
for( int i = 0; i < 10; ++i )
{
Book b( i );
mapBooks[ i ] = b;
}
The statement causing the error is:
mapBooks[ i ] = b;
If I add a default constructor, the error does not appear. However, I don't understand why the need. Can anyone explain? If I use insert()
, the problem does not appear.
By the way, I'm using Visual C++ 2008 to compile.