本文介绍了我是唯一一个疯狂的人吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 以下代码不起作用: 01:A级{ 02:公开: 03 :bool f(int x) 04:{ 05:返回x> 1; 06:} 07:}; 08: 09:B级:公开A { 10:公开: 11:bool f() 12:{ 13:返回f(3); 14:} 15:}; 16: 17:int main() 18:{} 编译器报告以下错误: g ++ proba.cpp -o proba proba.cpp:在成员函数`bool B :: f()中'': proba.cpp:13:错误:没有匹配函数来调用`B :: f(int)'' proba.cpp:12:注意:候选人是:bool B :: f() make:*** [proba]错误1 因为我认为你打算提议推杆A :: f(3),我可以告诉 你我已经尝试过这样做了。但是: *为什么我必须这样做!?方法完全不同 有不同的参数。 *如果我这样做,那么下面的代码仍然不起作用: 01:A级{ 02:公开: 03:bool f(int x) 04:{ 05:返回x> 1; 06:} 07:}; 08: 09:B级:公开A { 10:公开: 11:bool f() 12:{ 13:返回A :: f(3); 14:} 15:}; 16: 17:int main() 18:{ 19:B b; 20:bf(4) ; 21:} 给出以下错误: proba.cpp:在函数`int main()''中: proba.cpp:20:错误:没有匹配函数来调用`B :: f(int)'' proba.cpp:12:注意:候选人是:bool B :: f() make:*** [proba]错误1 现在你怎么看?就个人而言,它打破了我的C ++ 的信心。 问候, Darko The following code doesn''t work: 01: class A {02: public:03: bool f( int x )04: {05: return x>1;06: }07: };08:09: class B: public A {10: public:11: bool f()12: {13: return f(3) ;14: }15: };16:17: int main()18: {} Compiler reports the following error:g++ proba.cpp -o probaproba.cpp: In member function `bool B::f()'':proba.cpp:13: error: no matching function for call to `B::f(int)''proba.cpp:12: note: candidates are: bool B::f()make: *** [proba] Error 1 Since I suppose you are going to propose putting A::f(3), I can tellyou I''ve already tried to do that and it works. But:* Why would I have to do that!? The methods are perfectly distincthaving different arguments.* If I do that, then the following code still doesnt work: 01: class A {02: public:03: bool f( int x )04: {05: return x>1;06: }07: };08:09: class B: public A {10: public:11: bool f()12: {13: return A::f(3) ;14: }15: };16:17: int main()18: {19: B b;20: b.f( 4 );21: }giving the following error: proba.cpp: In function `int main()'':proba.cpp:20: error: no matching function for call to `B::f(int)''proba.cpp:12: note: candidates are: bool B::f()make: *** [proba] Error 1 Now what do you think about that? Personally, it broke my C++confidence. Regards, Darko推荐答案 阅读有关重载的信息与隐藏相比。 Read about "overloading" versus "hiding". 没什么特别的。这是代码中的错误。您可以通过以下方式修复 bA :: f(4); Nothing in particular. It''s the error in your code. You can fixit by doing b.A::f(4); 开始时是否有信心? 一旦你理解了重载之间的区别, " hidden"和overriding,你会知道该怎么做。你也可能想要了解名字查询,虽然这不是一个简单的 话题。 V - 请在通过电子邮件回复时删除资金''A' 我没有回复顶部-posted回复,请不要问 Was there confidence to begin with? As soon as you understand the difference between "overloading","hiding", and "overriding", you''ll know what to do. You also mightwant to learn about "name lookup", although it''s not really a simpletopic. V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask 作为一项决议,您可能希望使用A :: f;在B类中, 派生类。这会导致b.f()寻找匹配的一组 重载中的基类重载。它会找到 它。 As a resolution, you may want to put using A::f; in class B, thederived class. This brings the base class overload in the set ofoverloads that b.f() will look for to find a match. And it will findit. 阅读有关重载的信息与隐藏相比。 Read about "overloading" versus "hiding". 没什么特别的。这是代码中的错误。您可以通过以下方式修复 bA :: f(4); Nothing in particular. It''s the error in your code. You can fixit by doing b.A::f(4); 开始时有信心吗? 一旦你理解了重载之间的区别, " hidden"和overriding,你会知道该怎么做。你也可能想要了解名字查询,虽然这不是一个简单的 话题。 V - 请在通过电子邮件回复时删除资金''A' 我没有回复顶部-posted回复,请不要问 Was there confidence to begin with?As soon as you understand the difference between "overloading","hiding", and "overriding", you''ll know what to do. You also mightwant to learn about "name lookup", although it''s not really a simpletopic.V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask 谢谢,隐藏术语救了我。我在互联网上查了一下, 并猜测:你不必使用那种丑陋的结构: bA :: f(4); 但是可以这样说: 在B类中使用A :: f; ,然后一切正常。 我还必须提到重写没有 与此有关,因为这里没有重写类方法, 和如果重载效果很好,那么就不存在任何问题。 你可能会注意到,f()和f(int)完全相互重叠, 因为它们有不同的参数的数量,虽然参数 类型也足够了;所以,基于我以前的知识 应该没有问题。因此,我发现你的光顾口气 令人反感并且毫无意义。你曾帮我隐藏,但是,我从来都不知道,所以感谢和 有一个美好的一天! :) Darko Thanks, the "hiding" term saved me. I looked it up on the Internet,and guess what: you don''t have to use that ugly construction:b.A::f(4);but can say something like this:using A::f;in the class B, and then everything works fine. I must also mention that overriding doesn''t haveanything to do with this, since no class method is overridden here,and if overloading worked well, then no issue would exist.As you may notice, f() and f(int) overload each other perfectly,because they have a different number of arguments, although argumenttypes would suffice, too; so, based on my previous knowledgeno problem should occurr. Thus, I found your patronizing toneoffensive and senseless. You have helped me with "hiding", though,which I never knew occurred in the first place, so thanks andhave a good day! :) Darko 这篇关于我是唯一一个疯狂的人吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 02:18