模糊的C ++编译错误 [英] Nebulous C++ Compile Error
问题描述
任何人都可以对此提出建议
Can anyone advise on this:
In file included from recorders/r5000channel.h:13:0,
from recorders/r5000channel.cpp:11:
recorders/dtvchannel.h:53:18: warning: ‘virtual bool DTVChannel::Tune(const IPTVTuningData&)’ was hidden [-Woverloaded-virtual]
virtual bool Tune(const IPTVTuningData&) { return false; }
^
In file included from recorders/r5000channel.cpp:11:0:
recorders/r5000channel.h:29:18: warning: by ‘virtual bool R5000Channel::Tune(const DTVMultiplex&, QString)’ [-Woverloaded-virtual]
virtual bool Tune(const DTVMultiplex &/*tuning*/, QString /*inputname*/)
^
In file included from recorders/r5000channel.h:13:0,
from recorders/r5000channel.cpp:11:
recorders/dtvchannel.h:65:18: warning: ‘virtual bool DTVChannel::Tune(const QString&, int)’ was hidden [-Woverloaded-virtual]
virtual bool Tune(const QString &freqid, int finetune)
^
In file included from recorders/r5000channel.cpp:11:0:
recorders/r5000channel.h:29:18: warning: by ‘virtual bool R5000Channel::Tune(const DTVMultiplex&, QString)’ [-Woverloaded-virtual]
virtual bool Tune(const DTVMultiplex &/*tuning*/, QString /*inputname*/)
^
In file included from recorders/r5000channel.h:13:0,
from recorders/r5000channel.cpp:11:
recorders/dtvchannel.h:71:18: warning: ‘virtual bool DTVChannel::Tune(uint64_t, QString)’ was hidden [-Woverloaded-virtual]
virtual bool Tune(uint64_t frequency, QString inputname)
^
In file included from recorders/r5000channel.cpp:11:0:
recorders/r5000channel.h:29:18: warning: by ‘virtual bool R5000Channel::Tune(const DTVMultiplex&, QString)’ [-Woverloaded-virtual]
virtual bool Tune(const DTVMultiplex &/*tuning*/, QString /*inputname*/)
r5000channel.h- https://pastee.org/rjajf
r5000channel.h - https://pastee.org/rjajf
r5000channel.cpp- https://pastee.org/qangy
r5000channel.cpp - https://pastee.org/qangy
推荐答案
请注意,这不是错误,而是警告.看来您的基类包含名为 Tune
的 virtual
函数的重载版本:当您覆盖其中的一个时,使用派生对象时,基类中的所有版本都会被隐藏.通常,这是无意的,处理重载的 virtual
函数的正常方法是使 virtual
函数得到 protected
,从 public
转发(可能还有 inline
)功能.参见例如 std :: num_get< ...>
中的 virtual
函数.
Note that it isn't an error but a warning. It seems your base classes contain overloaded version of the virtual
function named Tune
: when you override one of them, all versions in bases are hidden when using a derived object. Normally, this is unintentional and the normal approach to deal with overloaded virtual
functions is to make the virtual
functions protected
, delegating to them from a public
forwarding (and probably inline
) function. See, e.g., the virtual
functions in std::num_get<...>
.
另一种避免隐藏的方法是提供 using
声明以及 virtual
函数的替代.就个人而言,我更喜欢转发方法.
Another approach to avoid the hiding is to provide using
-declarations together with the overrides of virtual
functions. Personally, I prefer the forwarding approach.
显示的警告实际上来自头文件.这意味着您使用的库是草率的,或者您创建的草率基类有些草率.
The warnings shown are actually from header files. This means that either the library you are using is sloppy or you created a somewhat sloppy base class.
这是 SSCCE ,其中显示了问题,并在注释中给出了解决方法:
Here is an SSCCE showing the problem and, in a comment, the fix:
struct base
{
virtual void foo(int) {}
virtual void foo(bool) {}
};
struct derived: base
{
virtual void foo(int) {}
// add this: using base::foo;
};
int main()
{
}
使用上述代码编译上面的代码时,会收到以下消息(缩写为仅显示编译器版本和警告):
When compiling the above code with I get these messages (abbreviated to only show the compiler version and the warnings):
$ g++ -v -c -Woverloaded-virtual overloaded-virtual.cpp
[...]
GNU C++ (GCC) version 4.9.0 20131031 (experimental) (x86_64-apple-darwin13.0.0)
compiled by GNU C version 4.9.0 20131102 (experimental), GMP version 5.0.5, MPFR version 3.1.1, MPC version 1.0.1
[...]
overloaded-virtual.cpp:4:22: warning: ‘virtual void base::foo(bool)’ was hidden [-Woverloaded-virtual]
virtual void foo(bool) {}
^
overloaded-virtual.cpp:9:22: warning: by ‘virtual void derived::foo(int)’ [-Woverloaded-virtual]
virtual void foo(int) {}
^
添加评论时,警告消失(如预期).
When adding the comment, the warnings go away (as expected).
这篇关于模糊的C ++编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!