繁体   English   中英

C ++模板类继承

[英]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

问题是这样的代码

文件A.h

template<typename T, int dim>
class A
{
 public:
  A(){};
  ~A(){};
 protected:
  void foo1(){};
}

文件库

#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 ++进行最终发行。

如果有人可以提供任何建议,我将非常高兴。

foo1不是依赖表达式,因此不使用基类(即依赖类型)来解析foo1调用。

由于您无法更改代码,因此已被塞满。 如果可以更改代码,则需要将表达式更改为从属。 通常,这是通过将其更改为this->foo1()

这是模板的一个众所周知的问题。 C ++常见问题解答中有解释

在gcc 4.4.1(操作系统为Ubuntu)版本上,可以通过使用-fpermissive选项将编译错误转换为编译警告。

编辑:某些编译器接受它的事实,并不意味着它将在将来的版本中继续接受它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM