1

I'm trying to add up two objects together even if they contain the same variables. Im doing this through overloading operator += but I can't manage to return the newly temp object created inside the unionOfBag function. I only get the same default obj but not the addition.

CLASS FILE:

#pragma once
#include "BagInterface.h"
#include <iostream>
#include <vector>
using namespace std;


static const int DEFAULT_CAPACITY = 20;

template <class ItemType>
class ArrayBag : public BagInterface<ItemType> {

private:

    //ItemType *aptr;
    ItemType items[DEFAULT_CAPACITY];
    int itemCount;
    int maxItems;

    int getIndexOf(const ItemType &target, int searchIndex) const;
    int countFrequency(const ItemType &target, int searchIndex) const;

public:
    ArrayBag();
    //ArrayBag(const ArrayBag<ItemType> &aBag);
    int getCurrentSize() const;
    bool isEmpty() const;
    bool add(const ItemType &newEntry);
    bool remove(const ItemType &newEntry);
    void clear();
    bool contains(const ItemType &anEntry) const;
    int getFrequencyOf(const ItemType & anEntry) const;
    vector<ItemType>toVector() const;

    ArrayBag<ItemType> &unionOfBags(const ArrayBag<ItemType> &aBag) const;
    ArrayBag<ItemType> intersectionOfBags(const ArrayBag<ItemType> &aBag) const;
    ArrayBag<ItemType> differenceOfBags(const ArrayBag<ItemType> &aBag) const;

    ArrayBag<ItemType> &operator += (const ArrayBag<ItemType> &aBag);
    ItemType &operator [] (int subs);
};


template <class ItemType>
ArrayBag<ItemType>::ArrayBag() {

    itemCount = 0;
    maxItems = DEFAULT_CAPACITY;
}

//template <class ItemType>
//ArrayBag<ItemType>::ArrayBag(const ArrayBag<ItemType> &aBag) {
//
//  this->items = aBag.items;
//  this->itemCount = DEFAULT_CAPACITY;
//
//}

template <class ItemType>
bool ArrayBag<ItemType>::add(const ItemType &newEntry) {

    bool hasRoomToAdd = (itemCount < maxItems);

    if (hasRoomToAdd) {
        items[itemCount] = newEntry;
        itemCount++;
    }

    return hasRoomToAdd;
}

template <class ItemType>
vector<ItemType> ArrayBag<ItemType>::toVector() const {

    vector<ItemType> bagContents;

    for (int i = 0; i < itemCount; i++) {
        bagContents.push_back(items[i]);

    }

    return bagContents;
}

template<class ItemType>
int ArrayBag<ItemType>::getCurrentSize() const {

    return itemCount;
}

template<class ItemType>
bool ArrayBag<ItemType>::isEmpty() const {

    return itemCount == 0;
}

template <class ItemType>
bool ArrayBag<ItemType>::contains(const ItemType &anEntry) const {
    /*return getFrecuencyOf(target) > 0;*/

    bool found = false;
    int curIndex = 0;

    while (!found && (curIndex < itemCount)) {
        if (anEntry == items[curIndex])
            found = true;

        curIndex++;
    }

    return found;
}

template<class ItemType>
bool ArrayBag<ItemType>::remove(const ItemType &anEntry) {

    int locatedIndex = getIndexOf(anEntry, 0);
    bool canRemoveItem = !isEmpty() && (locatedIndex > -1);

    if (canRemoveItem) {
        itemCount--;
        items[locatedIndex] = items[itemCount];
    }

    return canRemoveItem;
}

template <class ItemType>
void ArrayBag<ItemType>::clear() {

    itemCount = 0;
}

template <class ItemType>
int ArrayBag <ItemType>::getIndexOf(const ItemType &target, int searchIndex) const {

    int result = -1;

    if (searchIndex < itemCount) {

        if (items[searchIndex] == target)
            result = searchIndex;
        else
            result = getIndexOf(target, searchIndex + 1);
    }


    return result;
}



template <class ItemType>
int ArrayBag<ItemType>::getFrequencyOf(const ItemType &anEntry) const {
    return countFrequency(anEntry, 0);
}

template <class ItemType>
int ArrayBag<ItemType>::countFrequency(const ItemType &target, int searchIndex) const {

    int frequency = 0;

    if (searchIndex < itemCount) {
        if (items[searchIndex] == target)
            frequency = 1 + countFrequency(target, searchIndex + 1);

        else
            frequency = countFrequency(target, searchIndex + 1);
    }

    return frequency;

}

//
//template<class ItemType>
//ItemType &ArrayBag<ItemType>::operator [](int subs) {
//
//  return aptr[subs];
//
//}

template<class ItemType>
ArrayBag<ItemType> &ArrayBag<ItemType>::operator +=(const ArrayBag<ItemType> &aBag) {

    this->itemCount = aBag.itemCount;

    for (int i = 0; i < aBag.itemCount; i++) {
        this->items[i] += aBag.items[i];
        this->itemCount++;
    }
    return(*this);
}



template<class ItemType>
ArrayBag<ItemType> &ArrayBag<ItemType>::unionOfBags(const ArrayBag<ItemType> &aBag) const{

    ArrayBag<ItemType> newBag;

    newBag += aBag;

    //for (int i = 0; i < aBag.getCurrentSize(); i++) {
    //  newBag.items[i] += aBag.items[i];
    //}

    return newBag;

}
template<class ItemType>
ArrayBag<ItemType> ArrayBag<ItemType>::intersectionOfBags(const ArrayBag<ItemType> &aBag) const {

}
template<class ItemType>
ArrayBag<ItemType> ArrayBag<ItemType>::differenceOfBags(const ArrayBag<ItemType> &aBag) const {

}

MAIN.CPP

#include<iostream>
#include<string>
#include"ArrayBag.h"

using namespace std;

void displayBag(ArrayBag<string> & bag){
    cout << "The bag contains " << bag.getCurrentSize()
        << " items:" << endl;
    vector<string>bagItems = bag.toVector();

    int numberofEntries = (int)bagItems.size();
    for (int i = 0; i < numberofEntries; i++)
    {
        cout << bagItems[i] << " ";
    }
    cout << endl << endl;
}

void bagTester(ArrayBag<string> & bag)
{
    cout << "isEmpty: returns " << bag.isEmpty()
        << "; should be 1 (true)" << endl;
    displayBag(bag);

    string items[] = { "one", "two", "three", "four", "five", "one" };
    cout << "Add 6 items to the bag: " << endl;
    for (int i = 0; i < 6; i++)
    {
        bag.add(items[i]);
    }
    displayBag(bag);

    cout << "isEmpty: returns " << bag.isEmpty()
        << "; should be o (false)" << endl;
    cout << "getCurrentSize: returns " << bag.getCurrentSize()
        << "; should be 6" << endl;
    cout << "Try to add another entry: add(\"extra\") returns "
        << bag.add("extra") << endl;
}

int main()
{
    ArrayBag<string> bag1;
    ArrayBag<string> bag2;
    ArrayBag<string> newBag;

    bag2.add("a");
    bag2.add("b");
    bag2.add("c");
    bag2.add("d");
    bagTester(bag1);

    bag1.unionOfBags(bag2);
    newBag = bag1;
    displayBag(newBag);

    cout << "All done!" << endl;

    return 0;
}
2
  • Do you want your += operator to add the individual elements together or to append the two arrays? Your current implementation seems to be trying to do both and not doing either correctly. Commented Mar 27, 2015 at 20:16
  • Im trying to append the two arrays. Like get all the items from object one and object two and add them to a new object. Example object 1 contains one, two, three, four etc and object 2 contains a, b, c, d to add them toegther to form an object with one, two, three, four... a, b, c, d. Commented Mar 27, 2015 at 20:19

1 Answer 1

2

You are returning a locally created object by reference. This is a BIG no no. When a function ends all local objects are destroyed. Since you returned a reference from a object that was destroyed what does that reference refer to?

If you want to return an object that is a local variable you will need to return it by value.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay I see what you mean. But does this have to be in both unionOfBag and operator += functions?
The += must return a reference to the left object to make something like the following possible: (left += right).someFunction(...). In unionOfBags you don't change neither left nor right, so you must return a newly created ArrayBag but not by reference but by value, so that your temporary object is copied out of the function to an object outside of the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.