您的位置:首页 > 编程语言 > C语言/C++

C++模板继承在g++编译中应注意的问题(if you use `-fpermissive', G++ will accept your code, but allowing the use ...)

2010-12-06 23:38 1291 查看
x.cc: In member function `int Derived<T>::g()':

x.cc:6: error: there are no arguments to `f' that depend on a template

parameter, so a declaration of `f' must be available

x.cc:6: error: (if you use `-fpermissive', G++ will accept your code, but

allowing the use of an undeclared name is deprecated)

To make the code valid either use this->f(), or Base<T>::f(). Using the -fpermissive flag will also

let the compiler accept the code, by marking all function calls for which no declaration is visible at the time

of definition of the template for later lookup at instantiation time, as if it were a dependent call. We do not

recommend using -fpermissive to work around invalid code, and it will also only catch cases where functions

in base classes are called, not where variables in base classes are used (as in the example above).

Note that some compilers (including G++ versions prior to 3.4) get these examples wrong and accept above

code without an error. Those compilers do not implement two-stage name lookup correctly.

基本意思是,在模板继承出现的时候,需要在子类中用this来标志从父类中继承过来的成员函数和变量的调用。不然用using声明也行。

在使用模板继承的时候,如子类中有调用父类的成员函数和变量的情况,则需要用用this来调用,或者使用using声明,否则会导致在linux
的G++ 上无法编译通过,错误提示会有如上错误信息。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐