本文介绍了重载[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有两个类:pair和Array。在Array中,我已经重载了这样的 子脚本运算符: pair& operator [](int index){ 返回对(索引); } 然而,它会导致编译错误说没有 适当的功能,当我这样做时会返回''对': 对a = myArray [0]; 似乎总是返回Array类型的引用。我怎么能让它返回另一种类型?谢谢, 史蒂夫 解决方案 您的函数声明(以及定义)说明了这一点。 尝试查看返回类型方法 - 对& operator [](int index){..}" - Rakesh Kumar **从我的电子邮件地址中删除nospamplz以获取我的真实电子邮件** 我知道这在语法上是不正确的但我是做更多的事情 比这更复杂,并确实返回对有效的 ''对''对象的有效引用,而不仅仅是上面显示的临时对象。但是,我的 问题只是这样:我怎样才能让它返回另一种类型? 史蒂夫 我知道这在语法上是不正确的,但我正在做更多的事情比这更复杂,它确实返回对有效''pair''对象的有效引用,而不仅仅是上面显示的临时对象。但是,我的问题只有这样:我怎样才能让它返回另一种类型? Steve - - Rakesh Kumar **从我的电子邮件地址中删除nospamplz以获取我的真实电子邮件** Hi, I have two classes: pair, and Array. In Array, I have overloaded thesubcript operator like this: pair& operator[] (int index) {return pair(index);} However, it results in a compilation error saying that there''s notsuitable funtion that returns a ''pair'' when I do: pair a = myArray[0]; It seems to be always returning a reference of type Array. How can Imake it return another type? Thanks, Steve 解决方案 Your function declaration ( and hence, the definition) says that .Try to look at the return type of the method - "pair& operator[] (intindex) { .. } " --Rakesh Kumar** Remove nospamplz from my email address for my real email ** I know this is syntactically incorrect but I''m doing something morecomplicated than this and it does return a valid reference to a valid''pair'' object, and not just a temporary as show above. However, myquestion is only this: how can I make it return another type? Steve I know this is syntactically incorrect but I''m doing something more complicated than this and it does return a valid reference to a valid ''pair'' object, and not just a temporary as show above. However, my question is only this: how can I make it return another type? Steve -- --Rakesh Kumar** Remove nospamplz from my email address for my real email ** 这篇关于重载[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-02 23:36