Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Add method (http://www.go4expert.com/forums/add-method-t2008/)

SparkyCH 26Nov2006 07:32

Add method
 
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);
          }
        }       
}


shabbir 27Nov2006 10:25

Re: Add method
 
Looks like but we cannot be sure unless we see implementation of the used functions

SparkyCH 28Nov2006 07:07

Re: Add method
 
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);
        }

                       
}


SparkyCH 28Nov2006 07:08

Re: Add method
 
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);
}


SparkyCH 28Nov2006 07:10

Re: Add method
 
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

SparkyCH 28Nov2006 07:22

Re: Add method
 
Oh and i found it not working so can anyone help me

shabbir 28Nov2006 10:56

Re: Add method
 
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.

SparkyCH 29Nov2006 04:37

Re: Add method
 
solved


All times are GMT +5.5. The time now is 20:07.