2014-05-10 159 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); 

回答