Add method

Discussion in 'C++' started by SparkyCH, Nov 26, 2006.

  1. SparkyCH

    SparkyCH New Member

    Joined:
    Oct 18, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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);
    	  }
    	}	
    }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,341
    Likes Received:
    379
    Trophy Points:
    83
    Looks like but we cannot be sure unless we see implementation of the used functions
     
  3. SparkyCH

    SparkyCH New Member

    Joined:
    Oct 18, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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);
    	}
    
    			
    }
     
  4. SparkyCH

    SparkyCH New Member

    Joined:
    Oct 18, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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);
    }
    
     
  5. SparkyCH

    SparkyCH New Member

    Joined:
    Oct 18, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  6. SparkyCH

    SparkyCH New Member

    Joined:
    Oct 18, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Oh and i found it not working so can anyone help me
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,341
    Likes Received:
    379
    Trophy Points:
    83
    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.
     
  8. SparkyCH

    SparkyCH New Member

    Joined:
    Oct 18, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice