just a quick one i have to an object to a list, this complies but is it correct?? Code: void MediaCatDb::add(const Entry& entry) { list<Entry>::iterator it; for (it = _entries.begin(); it != _entries.end(); ++it) { if (*it == entry) { cout << "Duplicate Entry Present... Not added" << endl; } else { _entries.push_back(entry); } } }
Well first of all here is the header file Code: class MediaCatDb { typedef list<Entry> Container; Container _entries; public: typedef Container::iterator iterator; typedef Container::const_iterator const_iterator; I have a funciton in a menu that allows me to do that Code: void Menu::addEntry() { cout << "Create entry and add to database....." << endl << endl; Entry entry; entry.read(); if(entry.isValid()) { _mediaCatDb.add(entry); } }
In entry class Code: Entry::Entry(const Media& media, const Entertainment& entertainment) { Entry entry; if (entry.isValid()) { _media = entry.getMedia()->createCopy(); _entertainment = entry.getEntertainment()->createCopy(); } } bool Entry::isValid() const { return _media != NULL && _entertainment != NULL; } bool Entry::operator ==(const Entry& obj) const { if (getEntertainment()->getEntertainmentType() != obj.getEntertainment()->getEntertainmentType()) { return false; } if (getMedia()->getMediaType() != obj.getMedia()->getMediaType()) { return false; } if (getEntertainment()->getTitle() != obj.getEntertainment()->getTitle()) { return false; } return true; } bool Entry::operator !=(const Entry& entry) const { return !(*this==entry); }
Media and entertainment objects are create like so in the Entry header Media *_media; Entertainment *_entertainment; _meda = new Cdom; _entertainment = new Game; Cdrom and Game are classes
Can you explain as to what is not working. I could not get after going through all the post as to what is the structure of your code.