Closed. This question needs details or clarity。它当前不接受答案。
想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
6年前关闭。
任何人都可以对此提出建议:
r5000channel.h-https://pastee.org/rjajf
r5000channel.cpp-https://pastee.org/qangy
当我用上面的代码编译时,得到以下消息(缩写为仅显示编译器版本和警告):
添加评论时,警告消失(如预期)。
想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
6年前关闭。
任何人都可以对此提出建议:
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.cpp-https://pastee.org/qangy
最佳答案
请注意,这不是错误,而是警告。看来您的基类包含名为virtual
的Tune
函数的重载版本:当您覆盖其中的一个时,使用派生对象时基中的所有版本都被隐藏。通常,这是无意的,处理重载的virtual
函数的通常方法是使virtual
函数protected
,从public
转发(可能是inline
)函数委派给他们。参见例如virtual
中的std::num_get<...>
函数。
避免隐藏的另一种方法是提供using
-声明以及virtual
函数的替代。就个人而言,我更喜欢转发方法。
显示的警告实际上来自头文件。这意味着您正在使用的库是草率的,或者您创建的草率基类有些草率。
这是显示问题的SSCCE,并在注释中给出了解决方法:
struct base
{
virtual void foo(int) {}
virtual void foo(bool) {}
};
struct derived: base
{
virtual void foo(int) {}
// add this: using base::foo;
};
int main()
{
}
当我用上面的代码编译时,得到以下消息(缩写为仅显示编译器版本和警告):
$ 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) {}
^
添加评论时,警告消失(如预期)。
关于c++ - 模糊的C++编译错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20206370/
10-12 16:07