C++模板类继承
我一直在移植一些很久以前写的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
template<typename T, int dim>
class A
{
public:
A(){};
~A(){};
protected:
void foo1(){};
}
FileB.h
#include "FileA.h"
template<typename T>
class B : public A<T, 2>
{
public:
B(){};
~B(){};
void foo(){ foo1(); }
}
main.cpp
#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++ 进行最终发布。
如果有人能提供任何建议,我会很高兴。
I have been porting some C++ code, which was written long time ago, and is normally compiled with Visual C++ (Visual Studio 7.1 version) and Intel C++ Compiler 11.0, the target platform is Linux (Suse x86-64), with GCC 4.3.2 and Intel C++ Compiler 11.1
The problem is that code like this
FileA.h
template<typename T, int dim>
class A
{
public:
A(){};
~A(){};
protected:
void foo1(){};
}
FileB.h
#include "FileA.h"
template<typename T>
class B : public A<T, 2>
{
public:
B(){};
~B(){};
void foo(){ foo1(); }
}
main.cpp
#include "FileB.h"
int main()
{
B<float> b = B<float>();
}
does not compile on Linux (Intel C++ 11.1, GCC 4.3.2), but perfectly compiles on Windows (Visual C++ 7.1, Intel C++ 11.0), althow it surely must not depend on platform.
GCC tells that if I change foo1() to foo1(T a) it will work (and it does), but I can not change the code, and have to use Intel C++ for final release.
I would be glad if anyone could help with any advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
foo1
不是依赖表达式,因此作为依赖类型的基类不用于解析foo1
调用。由于您无法更改代码,因此您已经被塞满了。如果您可以更改代码,则需要将表达式更改为依赖项。通常,这是通过将其更改为
this->foo1()
来完成的。foo1
is not a dependent expression so the base class, which is a dependent type, is not used to resolve thefoo1
call.As you can't change the code, you are stuffed. If you could change the code you would need to change the expression to be dependent. Typically this is done by changing it to
this->foo1()
.这是模板的一个众所周知的问题。 C++ 常见问题解答中对此进行了解释
This is a well-known problem with templates. It is explained in the C++ FAQ
在 gcc 4.4.1(操作系统是 Ubuntu)版本上,我可以通过使用编译器的
-fpermissive
选项将编译错误转换为编译警告。编辑:某些编译器接受它的事实并不意味着它将在未来版本中继续接受它。
On gcc 4.4.1 (os is Ubuntu) version I could turn the compile error into a compile warning, by using the
-fpermissive
option to the compiler.Edit: The fact that some compiler accept it, doesn't mean it will continue to accept it in future versions.