2

我什至不确定给这个问题取什么标题;希望代码将展示我正在尝试做的事情:

#include <string>
#include <list>
using namespace std;

template<typename A> class Alpha { public: A m_alpha_a; };
template<typename B> class Bravo { public: B m_bravo_b; };

template<> class Alpha<string> { public: string m_alpha_string; };

template<typename B>
template<> class Alpha<Bravo<B> > 
{ 
public: 
    Bravo<B> m_bravo_class;   // Line A
}; 

int main()
{
    Alpha<int> alpha_int;
    alpha_int.m_alpha_a= 4;

    Alpha<string> alpha_string;
    alpha_string.m_alpha_string = "hi";

    Alpha<Bravo<int> > alpha_bravo_int;
    alpha_bravo_int.m_bravo_class.m_bravo_b = 9;
};

Alpha<A>当 A 是任何类型时,我想写一个专业化Bravo<B>,但编译器说

'>' 标记
封闭类模板之前的无效显式特化未显式特化

(参考// Line A。)什么是正确的语法来做我想做的事?

4

2 回答 2

4

只需更换

template<typename B>
template<> class Alpha<Bravo<B> > 

template<typename B>
class Alpha<Bravo<B> > 

即在这里删除template<>

于 2010-05-14T19:22:15.937 回答
1

您的问题似乎是重复的模板规范。你只需要使用一个。像这样:

#include <string>                                                                                                                                                                                                          
#include <list>                                                                                                                                                                                                            
using namespace std;                                                                                                                                                                                                       

template<typename A>                                                                                                                                                                                                       
class Alpha { public: A m_alpha_a; };                                                                                                                                                                                      

template<typename B>                                                                                                                                                                                                       
class Bravo { public: B m_bravo_b; };                                                                                                                                                                                      

template<> class Alpha<string> { public: string m_alpha_string; };                                                                                                                                                         

template<typename B>                                                                                                                                                                                                       
class Alpha< Bravo<B> >                                                                                                                                                                                                    
{                                                                                                                                                                                                                          
public:                                                                                                                                                                                                                    
    Bravo<B> m_bravo_class;   // Line A                                                                                                                                                                                    
};                                                                                                                                                                                                                         

int main()                                                                                                                                                                                                                 
{                                                                                                                                                                                                                          
    Alpha<int> alpha_int;                                                                                                                                                                                                  
    alpha_int.m_alpha_a= 4;                                                                                                                                                                                                

    Alpha<string> alpha_string;                                                                                                                                                                                            
    alpha_string.m_alpha_string = "hi";                                                                                                                                                                                    

    Alpha<Bravo<int> > alpha_bravo_int;                                                                                                                                                                                    
    alpha_bravo_int.m_bravo_class.m_bravo_b = 9;                                                                                                                                                                           
};                                                                                                                                                                                                                         
于 2010-05-14T19:31:15.073 回答