2014-05-10 2 views
1

Я был назначен следующий шаблон:C++ указатель на функцию в шаблоне

#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; 
}; 

Далее, есть абстрактный класс продукта и три подкласса:

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; 
    }; 
}; 

Наконец, класс ProductsCatalog, который обрабатывает Каталог экземпляр:

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

Что у меня есть проблемы с методом инспекции:

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)); 
    } 
}; 

Я получаю следующее сообщение об ошибке:

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

Я пробовал все, что я мог думать, но безуспешно. Следующие работы, как задумано, но, очевидно, не абстрактно достаточно:

it->second->Product->Action(*it->second->Product); 

ответ

Смежные вопросы