2012-03-22 114 views
-5

我做了一个模板单例类,我也做了一个模板化的数据结构。C++中的模板继承

我的问题是;如何让我的模板化数据结构从单体继承,所以你只能有一个这种结构的浮点类型?

我测试了两个单独的,并没有发现任何问题。下提供 代码...(这就是问题所在)

​​

的问题是上面的代码不能正常工作,我得到了很多的错误。我无法得到它,无论我做什么模板模板单身课... 我是要求也许建议,或者如果上面的代码是不正确guidence?在编译时

#ifndef SINGLETON_H 
#define SINGLETON_H 

template <class Type> 
class Singleton 
{ 
    public: 
     virtual ~Singleton(); 
     Singleton(); 
     static Type* m_instance; 
}; 
template <class Type> 
Type* Singleton<Type>::m_instance = 0; 

#include "Singleton.cpp" 
#endif 

#ifndef SINGLETON_CPP 
#define SINGLETON_CPP 
#include "Singleton.h" 

template <class Type> 
Singleton<Type>::Singleton() 
{ 
} 

template <class Type> 
Singleton<Type>::~Singleton() 
{ 
} 

template <class Type> 
Type* Singleton<Type>::getInstance() 
{ 
    if(m_instance==nullptr) 
    { 
     m_instance = new Type; 
    } 
    return m_instance; 
} 

#endif 


#ifndef ABSTRACTRMANAGERS_H 
#define ABSTRACTRMANAGERS_H 

#include <vector> 
#include <map> 
#include <stack> 
#include "Singleton.h" 

template <class Type> 
class AbstractRManagers : public Singleton<AbstractRManagers<Type> > 
{ 
    public: 
     virtual ~AbstractRManagers(); 

     int insert(Type* type, std::string name); 
     Type* remove(int i); 
     Type* remove(std::string name); 
     Type* get(int i); 
     Type* getS(std::string name); 
     int get(std::string name); 
     int get(Type* i); 
     bool check(std::string name); 
     int resourceSize(); 
    protected: 

    private: 
     std::vector<Type*> m_resources; 
     std::map<std::string,int> m_map; 
     std::stack<int> m_freePos; 
}; 
#include "AbstractRManagers.cpp" 

#endif 

#ifndef ABSTRACTRMANAGERS_CPP 
#define ABSTRACTRMANAGERS_CPP 

#include "AbstractRManagers.h" 
template <class Type> 
int AbstractRManagers<Type>::insert(Type* type, std::string name) 
{ 
    int i=0; 
    if(!check(name)) 
    { 
     if(m_freePos.empty()) 
     { 
      m_resources.push_back(type); 
      i = m_resources.size()-1; 
      m_map[name] = i; 
     } 
     else 
     { 
      i = m_freePos.top(); 
      m_freePos.pop(); 
      m_resources[i] = type; 
      m_map[name] = i; 
     } 
    } 
    else 
     i = -1; 

    return i; 
} 
template <class Type> 
int AbstractRManagers<Type>::resourceSize() 
{ 
    return m_resources.size(); 
} 
template <class Type> 
bool AbstractRManagers<Type>::check(std::string name) 
{ 
    std::map<std::string,int>::iterator it; 

    it = m_map.find(name); 

    if(it==m_map.end()) 
     return false; 

    return true; 
} 

template <class Type> 
Type* AbstractRManagers<Type>::remove(std::string name) 
{ 


    Type* temp = m_resources[m_map[name]]; 
    if(temp!=NULL) 
    { 
     std::map<std::string,int>::iterator it; 
     it = m_map[name]; 
     m_resources[m_map[name]] = NULL; 
     m_freePos.push(m_map[name]); 
     delete (*it).second; 
     delete (*it).first; 

     return temp; 
    } 
    return NULL; 
} 

template <class Type> 
Type* AbstractRManagers<Type>::remove(int i) 
{ 
    if((i < m_resources.size())&&(i > 0)) 
    { 
     Type* temp = m_resources[i]; 
     m_resources[i] = NULL; 
     m_freePos.push(i); 
     std::map<std::string,int>::iterator it; 
     for(it=m_map.begin();it!=m_map.end();it++) 
     { 
      if((*it).second == i) 
      { 
       delete (*it).second; 
       delete (*it).first; 
       return temp; 
      } 
     } 
     return temp; 
    } 
    return NULL; 
} 
template <class Type> 
int AbstractRManagers<Type>::get(Type* i) 
{ 
    for(int i2=0;i2<m_resources.size();i2++) 
    { 
     if(i == m_resources[i2]) 
     { 
      return i2; 
     } 
    } 
    return -1; 
} 
template <class Type> 
Type* AbstractRManagers<Type>::get(int i) 
{ 
    if((i < m_resources.size())&&(i >= 0)) 
    { 
     return m_resources[i]; 
    } 
    return NULL; 
} 
template <class Type> 
Type* AbstractRManagers<Type>::getS(std::string name) 
{ 
    return m_resources[m_map[name]]; 
} 
template <class Type> 
int AbstractRManagers<Type>::get(std::string name) 
{ 
    return m_map[name]; 
} 
template <class Type> 
AbstractRManagers<Type>::~AbstractRManagers() 
{ 
} 

#endif 



#include "AbstractRManagers.h" 
struct b 
{ 
    float x; 
}; 
int main() 
{ 
    b* a = new b(); 
    AbstractRManagers<b>::getInstance()->insert(a,"a"); 


    return 0; 
} 

这个程序产生下一个错误:

所有的
1> main.cpp 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::stack<_Ty,_Container> &,const std::stack<_Ty,_Container> &)' : could not deduce template argument for 'const std::stack<_Ty,_Container> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\stack(166) : see declaration of 'std::operator <' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(124) : while compiling class template member function 'bool std::less<_Ty>::operator()(const _Ty &,const _Ty &) const' 
1>   with 
1>   [ 
1>    _Ty=std::string 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\map(71) : see reference to class template instantiation 'std::less<_Ty>' being compiled 
1>   with 
1>   [ 
1>    _Ty=std::string 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\xtree(451) : see reference to class template instantiation 'std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>' being compiled 
1>   with 
1>   [ 
1>    _Kty=std::string, 
1>    _Ty=int, 
1>    _Pr=std::less<std::string>, 
1>    _Alloc=std::allocator<std::pair<const std::string,int>>, 
1>    _Mfl=false 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\xtree(520) : see reference to class template instantiation 'std::_Tree_nod<_Traits>' being compiled 
1>   with 
1>   [ 
1>    _Traits=std::_Tmap_traits<std::string,int,std::less<std::string>,std::allocator<std::pair<const std::string,int>>,false> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\xtree(659) : see reference to class template instantiation 'std::_Tree_val<_Traits>' being compiled 
1>   with 
1>   [ 
1>    _Traits=std::_Tmap_traits<std::string,int,std::less<std::string>,std::allocator<std::pair<const std::string,int>>,false> 
1>   ] 
1>   c:\program files\microsoft visual studio 10.0\vc\include\map(81) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled 
1>   with 
1>   [ 
1>    _Traits=std::_Tmap_traits<std::string,int,std::less<std::string>,std::allocator<std::pair<const std::string,int>>,false> 
1>   ] 
1>   c:\users\chris\desktop\311\ideas\idea1\idea1\abstractrmanagers.h(28) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled 
1>   with 
1>   [ 
1>    _Kty=std::string, 
1>    _Ty=int 
1>   ] 
1>   c:\users\chris\desktop\311\ideas\idea1\idea1\abstractrmanagers.h(30) : see reference to class template instantiation 'AbstractRManagers<Type>' being compiled 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::stack<_Ty,_Container> &,const std::stack<_Ty,_Container> &)' : could not deduce template argument for 'const std::stack<_Ty,_Container> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\stack(166) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::stack<_Ty,_Container> &,const std::stack<_Ty,_Container> &)' : could not deduce template argument for 'const std::stack<_Ty,_Container> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\stack(166) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::deque<_Ty,_Alloc> &,const std::deque<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::deque<_Ty,_Alloc> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\deque(1725) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::deque<_Ty,_Alloc> &,const std::deque<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::deque<_Ty,_Alloc> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\deque(1725) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::deque<_Ty,_Alloc> &,const std::deque<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::deque<_Ty,_Alloc> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\deque(1725) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\xtree(1885) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\xtree(1885) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\xtree(1885) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\vector(1502) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\vector(1502) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\vector(1502) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\memory(2582) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\memory(2582) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\memory(2582) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\xutility(1356) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\xutility(1356) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\xutility(1356) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\xutility(1179) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\xutility(1179) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\xutility(1179) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\utility(318) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\utility(318) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string' 
1>   c:\program files\microsoft visual studio 10.0\vc\include\utility(318) : see declaration of 'std::operator <' 
1>c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(125): error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+5

除了单身人士和抽象管理者之外,还有什么问题? – 2012-03-22 10:53:16

+0

重新调整的问题,以便它可以帮助。 – 2012-03-22 10:59:30

+4

@ChrisCondy Martinho实际上指出,在几乎所有情况下使用单例都被视为反模式,并且拥有一个名为'SomethingManager'的类同样表明您的设计可以得到改进。 – 2012-03-22 11:00:35

回答

3

首先,我要单身避免一起。

话虽如此,看看CRTP。这就是你应该如何实现它。


错误在你的代码:

  1. no 'Type* Singleton<Type>::getInstance()' member function declared in class 'Singleton<Type>'
  2. error: 'std::string' has not been declared

你需要在你单身申报static Type* getInstance();,以及包括地方。

+0

为什么要避免单身?他们是一个有用的模式恕我直言。 – 2012-03-22 10:54:45

+2

@EdHeal:不,他们不是。 http://jalf.dk/singleton/ – sbi 2012-03-22 11:03:05

+0

我不同意。 – 2012-03-22 11:21:02