Question

I've been assigned the following template:

#include <map>

template <typename T> 
class Catalog { 
    struct Item { 
        //..
    };
    std::map<int, Item*> items;
public: 
    Catalog(void); 
    Catalog(const Catalog&); 
    ~Catalog(void); 
    bool IsEmpty(void) const;
    int Size() const; 
    void Add(T*); 
    T* Remove(T*); 
    T* Find(T*); 
    typedef void (T::*pFunc) (const T&); 
    void Inspection(pFunc) const; 
};

Next, there is an abstract Product class and three subclasses:

class Product {
protected:
    unsigned int _id;
    string _name;
public:
    Product(const int& id, const string& name) : _id(id), _name(name) {};
    virtual void Action(const Product& p) = 0;
    virtual int hashCode() {
        return _id*100;
    };
    unsigned int getId(void) const {return _id;};
    string getName(void) const {return _name;};    
};

class ProductA : public Product {
public:
    ProductA(const int& id, const string& name) : Product(id, name) {};
    virtual void Action(const Product& p) {
        cout << "ahoj" << endl;
    };
};

Finally, class ProductsCatalog that handles a Catalog instance:

class ProductsCatalog {
    Catalog<Product> catalog;
public: 
    //..
    void CatalogInspection(void) const {
        catalog.Inspection(&Product::Action);
    }
};

What I have trouble with is the Inspection method:

template <typename T> void Catalog<T>::Inspection(pFunc p) const {  
    for (std::map<int, Item*>::const_iterator it=items.begin(); it!=items.end(); ++it)  {
        it->second->Product->*p(*(it->second->Product));
    }
};

I am getting the following error:

error C2064: term does not evaluate to a function taking 1 arguments

I've tried everything I could think of, without success. The following works as intended, but is obviously not abstract enough:

it->second->Product->Action(*it->second->Product);
Was it helpful?

Solution

Did you try (it->second->Product->*p)(*(it->second->Product)); for calling the method? The thread Calling C++ class methods via a function pointer seems to be related.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top