Arraybag Unions

I got the error: Error:class"Arraybag<int>"has no member "Union", I don't know how to resolve it.


---------------------------------------------------------
/** Header file for an array-based implementation of the ADT bag.
@file ArrayBag.h */

#pragma once

#include "BagInterface.h"

template<class ItemType>
class ArrayBag : public BagInterface<ItemType>
{
private :
static const int DEFAULT_CAPACITY = 6;// Small size to test for a full bag
ItemType items[DEFAULT_CAPACITY]; // Array of bag items
int itemCount; // Current count of bag items
int maxItems; // Max capacity of the bag
// Returns either the index of the element in the array items that
// contains the given target or -1, if the array does not contain
// the target.
int getIndexOf(const ItemType &target) const;
public:
ArrayBag();
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType &newEntry);
bool remove(const ItemType &anEntry);
void clear();
bool contains(const ItemType &anEntry) const;
int getFrequencyOf(const ItemType &anEntry) const;
vector<ItemType> toVector() const;
}; // end ArrayBag

template<class ItemType>
ArrayBag<ItemType>::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY)
{
} // end default constructor


template<class ItemType>
bool ArrayBag<ItemType>::add(const ItemType &newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
} // end if
return hasRoomToAdd;
} // end add

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;
} // end toVector

template<class ItemType>
int ArrayBag<ItemType>::getCurrentSize() const
{
return itemCount;
} // end getCurrentSize


template<class ItemType>
bool ArrayBag<ItemType>::isEmpty() const
{
return itemCount == 0;
} // end isEmpty



template<class ItemType>
void ArrayBag<ItemType>::clear()
{
itemCount = 0;
} // end clear


template<class ItemType>
int ArrayBag<ItemType>:: getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int curIndex = 0; // Current array index
while (curIndex < itemCount)
{
if (items[curIndex] == anEntry)
{
frequency++;
} // end if
curIndex++; // Increment to next entry
} // end while
return frequency;
} // end getFrequencyOf


template<class ItemType>
bool ArrayBag<ItemType>::contains(const ItemType& anEntry) const
{
bool found = false;
int curIndex = 0; // Current array index
while (!found && (curIndex < itemCount))
{
if (anEntry == items[curIndex])
{
found = true;
} // end if
curIndex++; // Increment to next entry
} // end while
return found;
} // end contains

template<class ItemType>
int ArrayBag<ItemType>::getIndexOf(const ItemType& target) const
{
bool found = false;
int result = -1;
int searchIndex = 0;
// If the bag is empty, itemCount is zero, so loop is skipped
while (!found && (searchIndex < itemCount))
{
if (items[searchIndex] == target)
{
found = true;
result = searchIndex;
}
else
{
searchIndex++;
} // end if
} // end while
return result;
} // end get IndexOf

template<class ItemType>
bool ArrayBag<ItemType>::remove(const ItemType &anEntry)
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = (!(isEmpty()) && (locatedIndex > -1));
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
} // end if
return canRemoveItem;
} // end remove
-----------------------------------------------------------------

/** @?le BagInterface.h */

#pragma once
#include <vector>
using namespace::std;

template < class ItemType>
class BagInterface
{
public :
/** Gets the current number of entries in this bag.
@return The integer number of entries currently in the bag. */
virtual int getCurrentSize() const = 0;
/** Sees whether this bag is empty.
@return True if the bag is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Adds a new entry to this bag.
@post If successful, newEntry is stored in the bag and
the count of items in the bag has increased by 1.
@param newEntry The object to be added as a new entry.
@return True if addition was successful, or false if not. */
virtual bool add(const ItemType &newEntry) = 0;
/** Removes one occurrence of a given entry from this bag,
if possible.
@post If successful, anEntry has been removed from the bag
and the count of items in the bag has decreased by 1.
@param anEntry The entry to be removed.
@return True if removal was successful, or false if not. */
virtual bool remove(const ItemType &anEntry) = 0;
/** Removes all entries from this bag.
@post Bag contains no items, and the count of items is 0. */
virtual void clear() = 0;
/** Counts the number of times a given entry appears in bag.
@param anEntry The entry to be counted.
@return The number of times anEntry appears in the bag. */
virtual int getFrequencyOf(const ItemType &anEntry) const = 0;
/** Tests whether this bag contains a given entry.
@param anEntry The entry to locate.
@return True if bag contains anEntry, or false otherwise. */
virtual bool contains(const ItemType &anEntry) const = 0;
/** Empties and then f ills a given vector with all entries that are in this bag.
@return A vector containing all the entries in the bag. */
virtual vector<ItemType> toVector() const = 0;
// end BagInterface


///////////////////////////////////////////////////////////////////////////////////////
/** Creates a new bag that combines the contents of this bag and a second
given bag without affecting the original two bags.
@param anotherBag The given bag.
@return A bag that is the union of the two bags. */

template < class ItemType>
class BagInterface
{
public :
BagInterface<ItemType> union(BagInterface<ItemType> anotherBag);///funcion union anadida///
};

};
------------------------------------------------------------------------------
///////////@Arraybag main///////
#include <iostream>
#include <vector>
using namespace::std;
#include "ArrayBag.h"
#include <stdio.h>

void displayBag(ArrayBag<int> &bag)
{
cout<< "The bag contains " << bag.getCurrentSize() << " items:" << endl;
vector<int> bagItems = bag.toVector(); // Lo pasa a un vector dinamico
int numberOfEntries = (int)bagItems.size();
for (int i = 0; i < numberOfEntries; i++)
{
cout<< bagItems[i] << " ";
} // end for
cout<< endl << endl;
} // end displayBag

int main()
{

ArrayBag<int> bag;
int valor;

ArrayBag<int> bag2;

ArrayBag<int> bag3;


if (bag.isEmpty())
cout<<"Esta vacio\n";
else
cout<<"No esta vacio, y tiene "<<bag.getCurrentSize()<<"\n";
bag.add(23);
bag.add(34);
bag.add(12);
bag.add(12);
bag.add(11);
bag.add(10);

displayBag(bag);
if (bag.isEmpty())
cout<<"Esta vacio\n";
else
cout<<"No esta vacio, y tiene "<<bag.getCurrentSize()<<"\n";
valor = 12;
cout<<"El 12 esta repetido :"<<bag.getFrequencyOf(valor)<<" veces\n";
valor = 34;
if(bag.contains(valor))
{
cout<<"El" <<valor<< "esta dentro del arreglo "<<endl;
}
else
{
cout<<"Error, no esta dentro del arreglo";
}


valor = 34;
bag.remove(valor);
if (bag.isEmpty())
cout<<"Esta vacio\n";
else
cout<<"No esta vacio, y tiene "<<bag.getCurrentSize()<<"\n";
displayBag(bag);
valor = 23;
bag.remove(valor);
if (bag.isEmpty())
cout<<"Esta vacio\n";
else
cout<<"No esta vacio, y tiene "<<bag.getCurrentSize()<<"\n";
displayBag(bag);
bag.clear();
if (bag.isEmpty())
cout<<"Esta vacio\n";
else
cout<<"No esta vacio, y tiene "<<bag.getCurrentSize()<<"\n";

cout<<"Este es el nuevo bag "<<endl; ///////new bag//

bag.add(23);
bag.add(34);
bag.add(12);
bag2.add(12);
bag2.add(11);
bag2.add(10);
bag3 = bag3.Union(bag, bag2);
displayBag(bag3);

system("pause");
return 0;
}//end main
C++ is case-sensitive so Union is not the same as union.
I write union instead of Union and got the same error: Error:class"Arraybag<int>"has no member "union" ... How can I fixed?
You have a class template BagInterface inside the class template BagInterface. It's probably not what you want.
I'm lost where I should put class template BagInterface?
You really should use code tags so that it is possible to refer to line numbers and it also keeps the formatting intact which makes the code easier to read.

1
2
3
4
5
6
7
8
9
10
11
12
13
template < class ItemType>
class BagInterface // This is the BagInterface that Arraybag inherits from.
{
public :
	// Here you have a whole bunch of functions and comments

	template < class ItemType>
	class BagInterface  // This is the BagInterface that has union. 
	{
	public :
		BagInterface<ItemType> union(BagInterface<ItemType> anotherBag);///funcion union anadida///
	};
};


I don't understand what's the point of having BagInterface inside BagInterface like that. I suspect it's a mistake.
Since union is a keyword in C++ you cannot use it as a function name.
You can change union to Union because C++ is case sensitive
Last edited on
Yes, It was a mistake , I delete that, now where I should put Union function and what should be the Union declaration instead of union.
Topic archived. No new replies allowed.