C++ template class inheritance
我一直在移植一些很久以前编写的C代码,通常是使用Visual C(Visual Studio 7.1版)和Intel C Compiler 11.0编译的,目标平台是Linux(Suse x86-64),带有GCC 4.3.2和Intel C编译器11.1
问题是这样的代码
FileA.h
1 2 3 4 5 6 7 8 9 | template<typename T, int dim> class A { public: A(){}; ~A(){}; protected: void foo1(){}; } |
FileB.h
1 2 3 4 5 6 7 8 9 | #include"FileA.h" template<typename T> class B : public A<T, 2> { public: B(){}; ~B(){}; void foo(){ foo1(); } } |
main.cpp
1 2 3 4 5 | #include"FileB.h" int main() { B<float> b = B<float>(); } |
不能在Linux(Intel C 11.1,GCC 4.3.2)上编译,但是可以在Windows(Visual C 7.1,Intel C 11.0)上完美编译,因此它一定不能依赖于平台。
GCC告诉我,如果我将foo1()更改为foo1(T a),它将可以工作(并且可以),但是我无法更改代码,必须使用Intel C进行最终发行。
如果有人可以提供任何建议,我会很高兴。
由于您无法更改代码,因此已被塞满。如果可以更改代码,则需要将表达式更改为从属。通常,这是通过将其更改为
来完成的。
这是模板的众所周知的问题。在C FAQ
中对此进行了解释
在gcc 4.4.1(操作系统为Ubuntu)版本上,可以通过对编译器使用
编辑:某些编译器接受它的事实,并不意味着它将在将来的版本中继续接受它。