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 ...