Hello
I have a Container class that will allow me to store & retrieve elements from it easily by using the member variables of the elements as their key/identifier.
My problem is I am tring to store a pointer to an objects member variable, but I get a compiler error. What am I doing wrong?
PS: do you have any advice on how I could improve my Container class; such as using const correctly & whether to hide the copy constructor?
This is how I use my container class:
#include <iostream>
#include "ObjectCollection.h"
using namespace std;
// Test items
struct FoodItem
{
unsigned int ID;
string name;
double price;
};
struct Order
{
unsigned int ID;
unsigned int personID;
};
int main()
{
Collection <FoodItem*, std::string> foodCol( &FoodItem::name );
Collection <Order*, unsigned int> orderCol( &Order::personID );
// Create some test objects
string nNames[] = {"a", "b", "c", "d"};
double nPrices[] = {1.1, 2.2, 3.3, 4.4};
unsigned int nPersonIDs[] = {6, 7, 8, 9};
for (int i=0; i<4; i++)
{
FoodItem *f = new FoodItem() { i, nNames[i], nPrices[i] };
Order *o = new Order() { i, nPersonIDs[i] };
foodCol.store( f );
orderCol.store( o );
}
// Test collection
if ( foodCol["a"]->ID == 0 )
{
cout << "Correctly retrieved a stored FoodItem by its name \n"
}
if ( foodCol[0]->name == "a" )
{
cout << "Correctly retrieved a stored FoodItem by its ID \n"
}
if ( foodCol[7]->ID == 1 )
{
cout << …