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

来源:互联网 发布:淘宝网 ie8不能登录 编辑:程序博客网 时间:2024/05/17 23:28

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

 

代码来自DTL文档index.htm

[cpp] view plaincopyprint?
  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. };  
这一段上次还碰到过TIMESTAMP_STRUCT未定义的情况,纯属意外

[cpp] view plaincopyprint?
  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. };  
在编译的时候,代码中只有个输出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.   

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


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

修改具体化代码

[cpp] view plaincopyprint?
  1. template<> class DefaultBCA<Example>  

为:

[cpp] view plaincopyprint?
  1. 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)      这里为什么要用继承,我也不知道,只是改了之后编译能过,起码先没错。



说明下:

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

原创粉丝点击