我做了一個模板單例類,我也做了一個模板化的數據結構。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 ==========
除了單身人士和抽象管理者之外,還有什麼問題? – 2012-03-22 10:53:16
重新調整的問題,以便它可以幫助。 – 2012-03-22 10:59:30
@ChrisCondy Martinho實際上指出,在幾乎所有情況下使用單例都被視爲反模式,並且擁有一個名爲'SomethingManager'的類同樣表明您的設計可以得到改進。 – 2012-03-22 11:00:35