【发布时间】:2010-06-02 09:32:47
【问题描述】:
我尝试大量使用模板来包装工厂类:
包装类(即classA)通过模板参数获取包装类(即classB)以提供“可插拔性”。
此外,我必须提供一个从包装的内部类 (innerB) 继承的内部类 (innerA)。
问题是g++“gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)”的以下错误信息:
sebastian@tecuhtli:~/Development/cppExercises/functionTemplate$ g++ -o test test.cpp
test.cpp: In static member function ‘static classA<A>::innerA<iB>* classA<A>::createInnerAs(iB&) [with iB = int, A = classB]’:
test.cpp:39: instantiated from here
test.cpp:32: error: dependent-name ‘classA::innerA<>’ is parsed as a non-type, but instantiation yields a type
test.cpp:32: note: say ‘typename classA::innerA<>’ if a type is meant
正如您在方法 createInnerBs 的定义中看到的,我打算传递一个非类型参数。所以typename的使用是错误的!
test.cpp的代码如下:
class classB{
public:
template < class iB>
class innerB{
iB& ib;
innerB(iB& b)
:ib(b){}
};
template<template <class> class classShell, class iB>
static classShell<iB>* createInnerBs(iB& b){
// this function creates instances of innerB and its subclasses,
// because B holds a certain allocator
return new classShell<iB>(b);
}
};
template<class A>
class classA{
// intention of this class is meant to be a pluggable interface
// using templates for compile-time checking
public:
template <class iB>
class innerA: A::template innerB<iB>{
innerA(iB& b)
:A::template innerB<iB>(b){}
};
template<class iB>
static inline innerA<iB>* createInnerAs(iB& b){
return A::createInnerBs<classA<A>::template innerA<> >(b); // line 32: error occurs here
}
};
typedef classA<classB> usable;
int main (int argc, char* argv[]){
int a = 5;
usable::innerA<int>* myVar = usable::createInnerAs(a);
return 0;
}
请帮助我,我已经面临这个问题好几天了。 这是不可能的,我想要做什么?还是我忘记了什么?
谢谢,Sema
【问题讨论】:
标签: c++