0
不幸的是我正在使用C++ 98。創建模板和基於boost :: shared_ptr的通用工廠編譯時出錯
template <class bT>
class Creator
{
public:
virtual bT* create() = 0;
};
template <class bT>
struct CreatorPtr
{
typedef boost::shared_ptr< Creator<bT> > ptr;
};
template <class bT, class cT>
class CreatorImpl : public Creator<bT>
{
public:
virtual bT* create() { return new cT; }
};
template <class bT>
class Factory
{
public:
virtual bT* create(const std::string& name) = 0;
virtual ~Factory() { _table_creator.clear(); }
protected:
Factory() {}
Factory(const Factory&) {}
Factory &operator=(const Factory&) { return *this; }
void registerCreator(const std::string& name, typename CreatorPtr<bT>::ptr creator)
{ _table_creator[name] = creator; }
typedef std::map<std::string, typename CreatorPtr<bT>::ptr> tableCreator;
typedef typename tableCreator::const_iterator citerTc;
......
protected:
tableCreator _table_creator;
};
我有錯誤
"error: expected nested-name-specifier before ‘tableCreator’" on the "typedef typename tableCreator::const_iterator citerTc;" line. I am using 4.1.2 g++."
對不起大家,我錯過了類型名在這裏「由SYAM指出」在citerTc的定義中刪除模板。現在代碼編譯並運行良好。 謝謝大家的幫助。