specialization of template.... in different namespace的解决

原文来自http://blog.csdn.net/coder_xia/article/details/6764777

 

代码来自DTL文档index.htm

  1. struct Example  
  2. {                                    // tablename.columnname:   
  3.     int exampleInt;                 // DB_EXAMPLE.INT_VALUE   
  4.     string exampleStr;              // DB_EXAMPLE.STRING_VALUE   
  5.     double exampleDouble;           // DB_EXAMPLE.DOUBLE_VALUE   
  6.     long exampleLong;               // DB_EXAMPLE.EXAMPLE_LONG   
  7.     TIMESTAMP_STRUCT exampleDate;   // DB_EXAMPLE.EXAMPLE_DATE   
  8. };  
struct Example
{                                    // tablename.columnname:
	int exampleInt;                 // DB_EXAMPLE.INT_VALUE
	string exampleStr;              // DB_EXAMPLE.STRING_VALUE
	double exampleDouble;           // DB_EXAMPLE.DOUBLE_VALUE
	long exampleLong;               // DB_EXAMPLE.EXAMPLE_LONG
	TIMESTAMP_STRUCT exampleDate;   // DB_EXAMPLE.EXAMPLE_DATE
};
这一段上次还碰到过TIMESTAMP_STRUCT未定义的情况,纯属意外

  1. template<>class dtl::DefaultBCA<Example>  
  2. {  
  3. public:  
  4.         void operator()(BoundIOs &cols, Example &rowbuf)  
  5.         {  
  6.             cols["INT_VALUE"] == rowbuf.exampleInt;  
  7.             cols["STRING_VALUE"] == rowbuf.exampleStr;  
  8.             cols["DOUBLE_VALUE"] == rowbuf.exampleDouble;  
  9.             cols["EXAMPLE_LONG"] == rowbuf.exampleLong;  
  10.             cols["EXAMPLE_DATE"] == rowbuf.exampleDate;  
  11.     }  
  12. };  
template<>class dtl::DefaultBCA<Example>
{
public:
    	void operator()(BoundIOs &cols, Example &rowbuf)
    	{
    		cols["INT_VALUE"] == rowbuf.exampleInt;
    		cols["STRING_VALUE"] == rowbuf.exampleStr;
    		cols["DOUBLE_VALUE"] == rowbuf.exampleDouble;
    		cols["EXAMPLE_LONG"] == rowbuf.exampleLong;
    		cols["EXAMPLE_DATE"] == rowbuf.exampleDate;
	}
};
在编译的时候,代码中只有个输出helloworld,错误提示如下:

error: specialization of ‘template<class DataObj> class dtl::DefaultBCA’ in different namespace

参考1:http://womble.decadent.org.uk/c++/template-faq.html#specialise-ns    faq,是个很和谐的东东啊

Q: Whatdoes the error message "specialization of ... in different namespace"mean?

A: Thismeans that the code appears to be defining a template specialisation, but itnames a template that was defined in a different namespace. This is not valid,though some older versions of g++ accept it. Every declaration for a templatemust be placed in the same namespace, just like repeated declarations of anyother named entity.


参考2:http://gcc.gnu.org/ml/gcc/2005-04/msg00134.html 

The error is that the specialisation isin a different namespace from  the declaration, not the definition.   

//他的观点是声明与模板的具体化在不同的命名空间了


先不去深究,直接上楼主的解决方案:

修改具体化代码

  1. template<> class DefaultBCA<Example>  
template<> class DefaultBCA<Example>

为:

  1. class Test:public DefaultBCA<Example>  
class Test:public DefaultBCA<Example>

即具体化模板类的时候,用类继承


注意点:

1)      template<>,也可以写在class那行的第二行,如果没有,也许会导致too few template-parameter-lists的错误;可以参考

http://hi.baidu.com/huangyunict/blog/item/5b50bdd658b8f32206088bd5.html

参考http://forum.ubuntu.org.cn/post-99237.html ,貌似是gcc版本的问题

2)      这里为什么要用继承,我也不知道,只是改了之后编译能过,起码先没错。



说明下:

        这是楼主经过查找和测试之后改的,由于对模板不是很熟,所以也搜了部分基础的东东,就没贴上来,其他主要参考的链接都给出了,应该够具体了。希望对以后有同样问题的人有用,不过只是对楼主自己遇到的情况而言,不保证其他情况。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值