原文来自http://blog.csdn.net/coder_xia/article/details/6764777
代码来自DTL文档index.htm
- 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
- };
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未定义的情况,纯属意外
- 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;
- }
- };
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.
//他的观点是声明与模板的具体化在不同的命名空间了
先不去深究,直接上楼主的解决方案:
修改具体化代码
- template<> class DefaultBCA<Example>
template<> class DefaultBCA<Example>
为:
- 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) 这里为什么要用继承,我也不知道,只是改了之后编译能过,起码先没错。
说明下:
这是楼主经过查找和测试之后改的,由于对模板不是很熟,所以也搜了部分基础的东东,就没贴上来,其他主要参考的链接都给出了,应该够具体了。希望对以后有同样问题的人有用,不过只是对楼主自己遇到的情况而言,不保证其他情况。