I have a cd , dvd. book media program. Currently, i am trying to add book information to a set. I am getting a nasty crash.
error: Unhandled exception at 0x00449d76 in a04xc.exe: 0xC0000005: Access violation reading location 0x00000014.
So, obviously its trying to access memory its not suppose to? just not sure why or what i need to do to add information?
Its coming from this line in this function...
const Item* Library::addBook(const string& title, const string& author, const int nPages)
{
Book* item = new Book(title,author,nPages);
allBooks.insert(item); // add to set of all books
allBooksByAuthor[author]->insert(item); // causing error..
return item;
}
here is the driver..
// add items to library
cout << ">>> adding items to library:\n\n";
item = library->addBook("The Curious Incident of the Dog in the Night-Time", "Mark Haddon", 240);
if (item != NULL) {
library->addKeywordForItem(item, "autism");
library->addKeywordForItem(item, "Asperger's Syndrome");
library->printItem(cout, item);
}
here is part of the library cpp where i am having problems with addbooks function
#include "Library.h"
#include "book.h"
ItemSet allBooks; // for my sets defined in the Items cpp
ItemSetMap allBooksByAuthor;
void Library::addKeywordForItem(const Item* item, const string& keyword)
{
//item->addKeyword(keyword);
}
const ItemSet* Library::itemsForKeyword(const string& keyword) const
{
return NULL;
}
void Library::printItem(ostream& out, const Item* const item) const
{
}
// book-related functions
const Item* Library::addBook(const string& title, const string& author, const int nPages)
{
Book* item = new Book(title,author,nPages);
allBooks.insert(item); // add to set of all books
allBooksByAuthor[author]->insert(item); // add to set of books by this author
return item;
}
here is the items class
#pragma once
#include <ostream>
#include <map>
#include <set>
#include <string>
#include "Item.h"
using namespace std;
typedef set<Item*> ItemSet;
typedef map<string,Item*> ItemMap;
typedef map<string,ItemSet*> ItemSetMap;
class Library
{
public:
// general functions
void addKeywordForItem(const Item* const item, const string& keyword);
const ItemSet* itemsForKeyword(const string& keyword) const;
void printItem(ostream& out, const Item* const item) const;
// book-related functions
const Item* addBook(const string& title, const string& author, int const nPages);
const ItemSet* booksByAuthor(const string& author) const;
const ItemSet* books() const;
// music-related functions
const Item* addMusicCD(const string& title, const string& band, const int nSongs);
void addBandMember(const Item* const musicCD, const string& member);
const ItemSet* musicByBand(const string& band) const;
const ItemSet* musicByMusician(const string& musician) const;
const ItemSet* musicCDs() const;
// movie-related functions
const Item* addMovieDVD(const string& title, const string& director, const int nScenes);
void addCastMember(const Item* const movie, const string& member);
const ItemSet* moviesByDirector(const string& director) const;
const ItemSet* moviesByActor(const string& actor) const;
const ItemSet* movies() const;
};
here is book.h
#ifndef BOOK_H
#define BOOK_H
#pragma once
#include "item.h"
using namespace std;
class Book : public Item
{
public:
Book(const string& title, const string& author, const int nPages);
~Book();
const int getPages() const;
const string getAuthor() const;
virtual void print(ostream& out) const;
private:
int numPages;
string Author;
};
ostream& operator<<(ostream& out, const Book* book);
#endif
I am just learning STL and i am having a hard time learning whats going on here. i understand set. I am just not sure in my situation map is for? is it adding item to author? and them adding them both to ItemSetMap?
typedef set<Item*> ItemSet;
typedef map<string,Item*> ItemMap;
typedef map<string,ItemSet*> ItemSetMap;
so, how can i fix this crash? am i not adding author to item correctly?
Thank You..