Like Tree2Likes
  • 1 Post By grumpy
  • 1 Post By laserlight

Need help finishing two operator overloading functions

This is a discussion on Need help finishing two operator overloading functions within the C++ Programming forums, part of the General Programming Boards category; I am wondering can some help to finish these two remaining operator overloading functions Also, "contents and NumItems are private" ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    17

    Need help finishing two operator overloading functions

    I am wondering can some help to finish these two remaining operator overloading functions

    Also, "contents and NumItems are private"

    Thanks

    Code:
    Bag operator+ (const Bag& b1, const Bag& b2);
    //Postcondition: the bag returned is the union of b1 and b2.
    
    
    ostream& operator<<(ostream&, const Bag&);
    //Overloading operator <<


    DynBag.cpp

    Code:
    #include <stdexcept>
    #include <iostream>
    #include "DynBag.h"
    
    
    
    
    Bag::Bag(int InitialCapacity) : NumItems(0)
    {
        capacity = InitialCapacity;
        contents = new ItemType[capacity];
    }
    
    
    Bag::Bag(const Bag& other) : NumItems(other.NumItems)
    {
        capacity = other.capacity;
        contents = new ItemType[capacity];
        for (int i=0; i < NumItems; ++i)
            contents[i] = other.contents[i];
    }
    
    
    
    
    int Bag::size () const
    {
        return NumItems;
    }
    
    
    int Bag::count_duplicates (const ItemType& target) const
    {
        int answer = 0;
    
    
        for (int i = 0; i < NumItems; ++i)
            if (target == contents[i])
                ++answer;
        return answer;
    }
    
    
    void Bag::insert(const ItemType & entry)
    {
        if (NumItems >= capacity)
            reserve(capacity*2);
    
    
        contents[NumItems] = entry;
        ++NumItems;
    }
    
    
    void Bag::erase_one(const ItemType & target) throw(domain_error)
    {
        int i, j;
    
    
        for (i = 0; i < NumItems; ++i)
            if (target == contents[i]) break;
    
    
        if (i == NumItems) throw domain_error("Target Not Found");
    
    
        if (i < NumItems-1) // target found but not last element
            for (j = i; j < NumItems-1; ++j)  // shift things up
                contents[j] = contents[j+1];
    
    
        --NumItems;
    
    
    }
    
    
    
    
    
    
    int Bag::erase(const ItemType & target)
    {
        int count = count_duplicates(target);
        int removed = 0;
    
    
        for ( ; removed < count; ++removed)
            erase_one(target);
    
    
        return(removed);
    
    
    }
    
    
    Bag& Bag::operator =(const Bag& other)
    {
        if (&other == this) return *this;
    
    
        delete[] contents;
        NumItems = other.NumItems;
        capacity = other.capacity;
        contents = new ItemType[capacity];
        for (int i=0; i < NumItems; ++i)
            contents[i] = other.contents[i];
        return *this;
    }
    
    
    void Bag::reserve (int new_capacity)
    {
        if (new_capacity == capacity) return;
    
    
        if (new_capacity < NumItems) new_capacity = NumItems;
    
    
        ItemType *new_contents = new ItemType[new_capacity];
    
    
        for (int i=0; i < capacity; ++i)
            new_contents[i] = contents[i];
    
    
        delete[] contents;
        contents = new_contents;
        capacity = new_capacity;
    }
    
    
    void Bag::print(ostream& out) const
    {
        for (int i = 0; i < NumItems; i++)
            out << contents[i] <<"\t";
    
    
        out <<endl;
    
    
    }
    
    
    Bag operator+ (const Bag& b1, const Bag& b2)
    {
    
    
    
    
    }
    
    
    ostream& operator<<(ostream&, const Bag&)
    {
    
    
    }
    DynBag.h

    Code:
    #ifndef DYNBAG_H
    #define DYNBAG_H
    
    
    #ifndef  _STRING_H_
    #include <string>
    #endif
    
    
    using namespace std;
    
    
    typedef string ItemType;
    
    
    class Bag {
    
    
        public:
    
    
        Bag (int InitialCapacity);
    
    
        Bag(const Bag& other);
    
    
        ~Bag();
    
    
        void insert(const ItemType& entry);
    
    
        void erase_one(const ItemType& target) throw (domain_error);
    
    
        int erase(const ItemType& target);
    
    
    
    
        Bag& operator=(const Bag& other);
        //Postcondition: Each item in other has been copied to this bag.
    
    
        Bag& operator+=(const Bag& addend);
        //Postcondition: Each item in addend has been added to this bag.
    
    
        void reserve(int new_capacity);
        //Postcondition: The bag's current capacity is changed to the new
        //capacity.
    
    
        //CONSTANT MEMBER FUNCTIONS
    
    
        int size() const;
        //Postcondition: the return value is the number of items in the Bag.
    
    
        int count_duplicates (const ItemType& target) const;
        //Poscondition: the return value is the number of times that target is
        //in the bag.
    
    
    
    
        void print (ostream& out) const;
        //Postcondition: The contents of the bag are written to standard
        //output.
    
    
        private:
            ItemType *contents;
            int NumItems;
            int capacity;
    };
    
    
    Bag operator+ (const Bag& b1, const Bag& b2);
    //Postcondition: the bag returned is the union of b1 and b2.
    
    
    ostream& operator<<(ostream&, const Bag&);
    //Overloading operator <<
    
    
    #endif

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,671
    Given that you have an operator+=(), even though you haven't shown the definition, the definition of operator+() should be easy.
    Code:
    Bag operator+(const Bag &b1, const Bag &b2)
    {
         Bag retval(b1);    // relies on a working copy constructor
         retval += b2;       // uses your operator+=()
         return retval;       //   relies on copy constructor being accessible
    }
    The operator<<() would presumably need to have a loop that outputs each element in the bag, sequentially. It can probably call the print() member function.
    iMalc likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    Quote Originally Posted by grumpy View Post
    Given that you have an operator+=(), even though you haven't shown the definition, the definition of operator+() should be easy.
    Code:
    Bag operator+(const Bag &b1, const Bag &b2)
    {
         Bag retval(b1);    // relies on a working copy constructor
         retval += b2;       // uses your operator+=()
         return retval;       //   relies on copy constructor being accessible
    }
    The operator<<() would presumably need to have a loop that outputs each element in the bag, sequentially. It can probably call the print() member function.



    Didn't work and getting these errors:

    http://s23.postimg.org/jq8h1548r/Untitled.jpg


    this is my main:

    Code:
    #include <stdexcept>
    #include <iostream>
    #include "DynBag.h"
    
    
    int main ()
    {
    	Bag b(2), b1(3), b2(2), b3(4);
    
    
    	b.insert("Lidiecita");
    	b.insert("Brendita");
    	b.insert("Francisquito");
    	b.insert("Lidiecita");
    	b1.insert("Mayi");
    	b1.insert("Eimy");
    
    
    	b += b;
    	b.print(cout);
    
    
    	b3 = b + b1;
    	b3.print(cout);
    
    
    	b2 = b1;
    	b2.print(cout);
    
    
    	Bag b4 = b3;
    	cout << b4;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by AmirHossein H-m
    Didn't work and getting these errors:

    http://s23.postimg.org/jq8h1548r/Untitled.jpg
    You should be able to copy and paste the error messages from the IDE and post them here in [code][/code] bbcode tags. As it stands, your image is so small that the text is unreadable.

    Don't do this:
    Code:
    #ifndef  _STRING_H_
    #include <string>
    #endif
    There will be a header inclusion guard in <string>, so let it do its job.

    Don't do this:
    Code:
    using namespace std;
    This is a header file, so there should be no using directives at file scope as they will then affect the source files in which this header is included.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by AmirHossein H-m
    Didn't work and getting these errors:

    http://s23.postimg.org/jq8h1548r/Untitled.jpg
    You should be able to copy and paste the error messages from the IDE and post them here in [code][/code] bbcode tags. As it stands, your image is so small that the text is unreadable.

    Don't do this:
    Code:
    #ifndef  _STRING_H_
    #include <string>
    #endif
    There will be a header inclusion guard in <string>, so let it do its job.

    Don't do this:
    Code:
    using namespace std;
    This is a header file, so there should be no using directives at file scope as they will then affect the source files in which this header is included.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    Quote Originally Posted by laserlight View Post
    You should be able to copy and paste the error messages from the IDE and post them here in [code][/code] bbcode tags. As it stands, your image is so small that the text is unreadable.

    Don't do this:
    Code:
    #ifndef  _STRING_H_
    #include <string>
    #endif
    There will be a header inclusion guard in <string>, so let it do its job.

    Don't do this:
    Code:
    using namespace std;
    This is a header file, so there should be no using directives at file scope as they will then affect the source files in which this header is included.

    Thanks , But my problem didn't solve ...

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Because you have not posted your updated code and corresponding error messages.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    17
    Code:
    C:\Users\Amir.Asus\Documents\assi6\DynBag.cpp||In function 'std::ostream& operator<<(std::ostream&, const Bag&)':|
    C:\Users\Amir.Asus\Documents\assi6\DynBag.cpp|124|warning: no return statement in function returning non-void [-Wreturn-type]|
    obj\Debug\Documents\assi6\Driver.o||In function `main':|
    C:\Users\Amir.Asus\Documents\assi6\Driver.cpp|18|undefined reference to `Bag::operator+=(Bag const&)'|
    C:\Users\Amir.Asus\Documents\assi6\Driver.cpp|21|undefined reference to `Bag::~Bag()'|
    C:\Users\Amir.Asus\Documents\assi6\Driver.cpp|28|undefined reference to `Bag::~Bag()'|
    C:\Users\Amir.Asus\Documents\assi6\Driver.cpp|28|undefined reference to `Bag::~Bag()'|
    C:\Users\Amir.Asus\Documents\assi6\Driver.cpp|28|undefined reference to `Bag::~Bag()'|
    C:\Users\Amir.Asus\Documents\assi6\Driver.cpp|28|undefined reference to `Bag::~Bag()'|
    obj\Debug\Documents\assi6\Driver.o:C:\Users\Amir.Asus\Documents\assi6\Driver.cpp|28|more undefined references to `Bag::~Bag()' follow|
    obj\Debug\Documents\assi6\DynBag.o||In function `ZplRK3BagS1_':|
    C:\Users\Amir.Asus\Documents\assi6\DynBag.cpp|117|undefined reference to `Bag::operator+=(Bag const&)'|
    C:\Users\Amir.Asus\Documents\assi6\DynBag.cpp|118|undefined reference to `Bag::~Bag()'|
    ||=== Build finished: 9 errors, 1 warnings (0 minutes, 0 seconds) ===|
    main.cpp

    Code:
    #include <stdexcept>
    #include <iostream>
    #include "DynBag.h"
    
    
    int main ()
    {
        Bag b(2), b1(3), b2(2), b3(4);
    
    
        b.insert("Lidiecita");
        b.insert("Brendita");
        b.insert("Francisquito");
        b.insert("Lidiecita");
        b1.insert("Mayi");
        b1.insert("Eimy");
    
    
        b += b;
        b.print(cout);
    
    
        b3 = b + b1;
        b3.print(cout);
    
    
        b2 = b1;
        b2.print(cout);
    
    
        Bag b4 = b3;
        cout << b4;
    }
    DynBag.cpp

    Code:
    #include <stdexcept>
    #include <iostream>
    #include "DynBag.h"
    
    
    
    
    Bag::Bag(int InitialCapacity) : NumItems(0)
    {
        capacity = InitialCapacity;
        contents = new ItemType[capacity];
    }
    
    
    Bag::Bag(const Bag& other) : NumItems(other.NumItems)
    {
        capacity = other.capacity;
        contents = new ItemType[capacity];
        for (int i=0; i < NumItems; ++i)
            contents[i] = other.contents[i];
    }
    
    
    
    
    int Bag::size () const
    {
        return NumItems;
    }
    
    
    int Bag::count_duplicates (const ItemType& target) const
    {
        int answer = 0;
    
    
        for (int i = 0; i < NumItems; ++i)
            if (target == contents[i])
                ++answer;
        return answer;
    }
    
    
    void Bag::insert(const ItemType & entry)
    {
        if (NumItems >= capacity)
            reserve(capacity*2);
    
    
        contents[NumItems] = entry;
        ++NumItems;
    }
    
    
    void Bag::erase_one(const ItemType & target) throw(domain_error)
    {
        int i, j;
    
    
        for (i = 0; i < NumItems; ++i)
            if (target == contents[i]) break;
    
    
        if (i == NumItems) throw domain_error("Target Not Found");
    
    
        if (i < NumItems-1) // target found but not last element
            for (j = i; j < NumItems-1; ++j)  // shift things up
                contents[j] = contents[j+1];
    
    
        --NumItems;
    
    
    }
    
    
    
    
    
    
    int Bag::erase(const ItemType & target)
    {
        int count = count_duplicates(target);
        int removed = 0;
    
    
        for ( ; removed < count; ++removed)
            erase_one(target);
    
    
        return(removed);
    
    
    }
    
    
    Bag& Bag::operator =(const Bag& other)
    {
        if (&other == this) return *this;
    
    
        delete[] contents;
        NumItems = other.NumItems;
        capacity = other.capacity;
        contents = new ItemType[capacity];
        for (int i=0; i < NumItems; ++i)
            contents[i] = other.contents[i];
        return *this;
    }
    
    
    void Bag::reserve (int new_capacity)
    {
        if (new_capacity == capacity) return;
    
    
        if (new_capacity < NumItems) new_capacity = NumItems;
    
    
        ItemType *new_contents = new ItemType[new_capacity];
    
    
        for (int i=0; i < capacity; ++i)
            new_contents[i] = contents[i];
    
    
        delete[] contents;
        contents = new_contents;
        capacity = new_capacity;
    }
    
    
    void Bag::print(ostream& out) const
    {
        for (int i = 0; i < NumItems; i++)
            out << contents[i] <<"\t";
    
    
        out <<endl;
    
    
    }
    
    
    Bag operator+(const Bag &b1, const Bag &b2)
    {
         Bag retval(b1);    // relies on a working copy constructor
         retval += b2;       // uses your operator+=()
         return retval;       //   relies on copy constructor being accessible
    }
    
    
    ostream& operator<<(ostream&, const Bag& b)
    {
    
    
    }

    DynBag.h

    Code:
    #ifndef DYNBAG_H
    #define DYNBAG_H
    
    
    
    
    #include <string>
    
    
    
    
    using namespace std;
    
    
    typedef string ItemType;
    
    
    class Bag {
    
    
        public:
    
    
        Bag (int InitialCapacity);
    
    
        Bag(const Bag& other);
    
    
        ~Bag();
    
    
        void insert(const ItemType& entry);
    
    
        void erase_one(const ItemType& target) throw (domain_error);
    
    
        int erase(const ItemType& target);
    
    
    
    
        Bag& operator=(const Bag& other);
        //Postcondition: Each item in other has been copied to this bag.
    
    
        Bag& operator+=(const Bag& addend);
        //Postcondition: Each item in addend has been added to this bag.
    
    
        void reserve(int new_capacity);
        //Postcondition: The bag's current capacity is changed to the new
        //capacity.
    
    
        //CONSTANT MEMBER FUNCTIONS
    
    
        int size() const;
        //Postcondition: the return value is the number of items in the Bag.
    
    
        int count_duplicates (const ItemType& target) const;
        //Poscondition: the return value is the number of times that target is
        //in the bag.
    
    
    
    
        void print (ostream& out) const;
        //Postcondition: The contents of the bag are written to standard
        //output.
    
    
        private:
            ItemType *contents;
            int NumItems;
            int capacity;
    };
    
    
    Bag operator+ (const Bag& b1, const Bag& b2);
    //Postcondition: the bag returned is the union of b1 and b2.
    
    
    ostream& operator<<(ostream&, const Bag& b1);
    //Overloading operator <<
    
    
    #endif
    It would be great if you could help me finish

    Code:
    Bag operator+ (constBag& b1, constBag& b2);
    ostream& operator<<(ostream&, constBag& b1);
    Thank you ...

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    This error message is telling you that your definition of operator<< is incompletely, and indeed the body of that function is empty:
    Code:
    C:\Users\Amir.Asus\Documents\assi6\DynBag.cpp||In function 'std::ostream& operator<<(std::ostream&, const Bag&)':|
    C:\Users\Amir.Asus\Documents\assi6\DynBag.cpp|124|warning: no return statement in function returning non-void [-Wreturn-type]|
    obj\Debug\Documents\assi6\Driver.o||In function `main':|
    This error message is telling you that you did not define operator+= at all:
    Code:
    C:\Users\Amir.Asus\Documents\assi6\Driver.cpp|18|undefined reference to `Bag::operator+=(Bag const&)'|
    This error message is telling you that you did not define the destructor:
    Code:
    C:\Users\Amir.Asus\Documents\assi6\Driver.cpp|21|undefined reference to `Bag::~Bag()'|
    iMalc likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,029
    Also, if possible, use std::vector instead of new.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help finishing a few functions.
    By Mo777 in forum C Programming
    Replies: 14
    Last Post: 03-09-2011, 06:04 PM
  2. operator new overloading and template functions
    By krappa in forum C++ Programming
    Replies: 40
    Last Post: 05-28-2008, 09:29 AM
  3. overloading operator help
    By syrel in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2004, 11:49 AM
  4. Replies: 3
    Last Post: 12-06-2002, 10:02 AM
  5. operator overloading
    By Akira in forum C++ Programming
    Replies: 1
    Last Post: 11-14-2001, 03:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21