Add method

Go4Expert Member
26Nov2006,07:32   #1
SparkyCH's Avatar
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);
	  }
	}	
}
Go4Expert Founder
27Nov2006,10:25   #2
shabbir's Avatar
Looks like but we cannot be sure unless we see implementation of the used functions
Go4Expert Member
28Nov2006,07:07   #3
SparkyCH's Avatar
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);
	}

			
}
Go4Expert Member
28Nov2006,07:08   #4
SparkyCH's Avatar
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);
}
Go4Expert Member
28Nov2006,07:10   #5
SparkyCH's Avatar
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
Go4Expert Member
28Nov2006,07:22   #6
SparkyCH's Avatar
Oh and i found it not working so can anyone help me
Go4Expert Founder
28Nov2006,10:56   #7
shabbir's Avatar
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.
Go4Expert Member
29Nov2006,04:37   #8
SparkyCH's Avatar
solved