Add method

SparkyCH's Avatar, Join Date: Oct 2006
Go4Expert Member
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);
	  }
	}	
}
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Looks like but we cannot be sure unless we see implementation of the used functions
0
SparkyCH's Avatar, Join Date: Oct 2006
Go4Expert Member
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);
	}

			
}
0
SparkyCH's Avatar, Join Date: Oct 2006
Go4Expert Member
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);
}
0
SparkyCH's Avatar, Join Date: Oct 2006
Go4Expert Member
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
0
SparkyCH's Avatar, Join Date: Oct 2006
Go4Expert Member
Oh and i found it not working so can anyone help me
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
SparkyCH's Avatar, Join Date: Oct 2006
Go4Expert Member
solved