本文介绍了连接呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我对C ++编程更新鲜,而且我只是想要学习级联通话,我已经 写了一个程序, class SetMe { public: void setX(int x){_ x = x;} void setY(int y){_ y = y ;} void doubleMe() { _x * = 2; _y * = 2; } 私人: int _x; int _y; }; int main(){ SetMe降低; ((lower.setX(20))。setY(30))。doubleMe( ); } 在Redhat Linux上编译gcc版本3.2.3时 它会出现以下错误, point.c:在函数`int main()''中: point.c:17:请求成员`setY'' `(& lower ) - > SetMe :: setX(int)(20)'',这是非聚合类型 `void'' 有人会解释如何删除这个错误。 我想保留串联电话的风格 谢谢 divya 解决方案 foodic写道:我对C ++编程很新鲜,而我只是想学习级联通话,我已经编写了一个程序, 类SetMe {公共: void setX(int x){_ x = x;} setX返回void! void setY(int y){_ y = y;} 所以setY。 void doubleMe() { _x * = 2; _y * = 2; } private: int _x; int _y; }; int main(){ SetMe lower; ((lower.setX(20) ).setY(30))doubleMe(); 这里你使用setX的返回值(无效)并调用setY。 你可能想要这样做。 SetMe& setX(int x){_ x = x;返回*这个;} SetMe& setY(int y){_ y = y;返回* this;} SetMe * doubleMe(){.... return * this;} } 在gcc 3.2.3版上编译时Redhat Linux 它给出了以下错误, point.c:在函数`int main()'': point.c:17:在`(& lower) - > SetMe :: setX(int)(20)'',这是非聚合类型`void''有人会解释如何删除此错误。我想保留连接调用的风格 通过引用返回对象。 foodic写道: 我对C ++编程更新鲜,我只是想学习级联通话,我已经编写了一个程序, 类SetMe {公共: void setX(int x){_ x = x;} void setY(int y){_ y = y;} void doubleMe() { _x * = 2; _y * = 2; } 私人: int _x; int _y; }; int main(){ SetMe lower; ((lower.setX(20))。setY (30))。doubleMe(); } 在Redhat Linux上编译gcc版本3.2.3时它会产生以下错误, point.c:在函数`int中main()'': point.c:17:请求成员`setY''`(& lower) - > SetMe :: setX(int)(20)'',哪个是非聚合类型`void''有人会解释如何删除这个错误。我想保留连接调用的风格 这个关键点是,每个函数都必须返回下一个调用可以作用的东西 。 如果你想做什么 SetMe降低; lower.setX(20).setY(30); 然后部分 lower.setX(20) 必须评估为可用作对象的东西 setY可以继续工作。最好的开始是 SetMe更低; SetMe someObj; someObj.setY(30); 现在用lower.setX替换someObj(20) lower.setX(20).setY(20); 之前与someObj具有相同的类型。因此,SetX必须返回SetMe 对象。 (由于显而易见的原因,我在SetX上做同样的修改, SetY和doubleMe同时) (注意:以下不是你想要的,但是关闭。 我在一分钟后回来了) class SetMe { public: SetMe setX(int x){_ x = x;返回*这个; } SetMe setY(int y){_ y = y;返回*这个; } SetMe doubleMe() { _x * = 2; _y * = 2; 返回* this; } 私人: int _x; int _y; }; 现在每个函数都返回一个SetMe对象。因此当 lower.SetX(20) 被评估时,其结果是一个SetMe对象,可以是 用于下次调用: lower.setX(20).doubleMe()。setY(30); 等待:确切的返回类型是什么每个功能? 这是''SetMe''。这意味着对于每次返回,返回一个副本 的对象。因此,在调用doubleMe的对象上面的 ,*不是* 更低,但是是一个对象,它是右下角的精确副本拨打SetX完成后。如果这不是你想要的b $ b,那么返回类型需要稍微修改。 我们想要的不是对象的副本,而是对象本身。 br /> 我们通过返回对象的引用来做到这一点: class SetMe { public : SetMe& setX(int x){_ x = x;返回*这个; } SetMe& setY(int y){_ y = y;返回*这个; } SetMe& doubleMe() { _x * = 2; _y * = 2; 返回*这个; } 私人: int _x; int _y; }; 现在,setX的返回对象是 用于调用setX的完全相同的对象(' 'this''是C ++的方式说' ''我'或''我''。唯一的问题是''this''是一种指针类型。 所以说''回复我''你必须取消引用指针,因此 返回* this;) lower.setX(20).setY(30); 对setX的调用现在返回对象的引用 跟注,这是''较低''。因此,setY将再次工作在''较低'' 并完成其工作。 这就是全部秘密。 - Karl Heinz Buchegger kb ****** @ gascad.at " Karl Heinz Buchegger" < kb ****** @ gascad.at> ;, haber iletisinde sunlari yazdi:42 *************** @ gascad.at .. 。 < snip> lower.setX(20).setY(30); 你甚至可以从这样的类构造开始: SetMe()。setX(20).setY(30); //如果你不需要降低在这一行之后, //说最后将它作为参数传递给函数。 对setX的调用现在返回对它的对象的引用并开展工作。 这就是秘密。 - Karl Heinz Buchegger kb******@gascad.at i am fresher to C++ programming, and I justwant to learn Concatenating Calls, I havewritten a program,class SetMe {public:void setX(int x) {_x = x;}void setY(int y) {_y = y;}void doubleMe(){_x *= 2;_y *= 2;}private:int _x;int _y;};int main(){SetMe lower;((lower.setX(20)).setY(30)).doubleMe();}While compiling on gcc version 3.2.3 on Redhat Linuxit gives following errors,point.c: In function `int main()'':point.c:17: request for member `setY'' in`(&lower)->SetMe::setX(int)(20)'', which is of non-aggregate type`void''Would somebody explain how to remove this error.I want to retain the style of concatenating callsthanksdivya 解决方案 foodic wrote: i am fresher to C++ programming, and I just want to learn Concatenating Calls, I have written a program, class SetMe { public: void setX(int x) {_x = x;}setX returns void ! void setY(int y) {_y = y;}So does setY. void doubleMe() { _x *= 2; _y *= 2; } private: int _x; int _y; }; int main(){ SetMe lower; ((lower.setX(20)).setY(30)).doubleMe();Here you use the return value from setX (which is void) and call setY.You probably want to do this.SetMe & setX(int x) {_x = x; return *this;}SetMe & setY(int y) {_y = y; return *this;}SetMe * doubleMe() { .... return *this;} } While compiling on gcc version 3.2.3 on Redhat Linux it gives following errors, point.c: In function `int main()'': point.c:17: request for member `setY'' in `(&lower)->SetMe::setX(int)(20)'', which is of non-aggregate type `void'' Would somebody explain how to remove this error. I want to retain the style of concatenating callsReturn the object by reference.foodic wrote: i am fresher to C++ programming, and I just want to learn Concatenating Calls, I have written a program, class SetMe { public: void setX(int x) {_x = x;} void setY(int y) {_y = y;} void doubleMe() { _x *= 2; _y *= 2; } private: int _x; int _y; }; int main(){ SetMe lower; ((lower.setX(20)).setY(30)).doubleMe(); } While compiling on gcc version 3.2.3 on Redhat Linux it gives following errors, point.c: In function `int main()'': point.c:17: request for member `setY'' in `(&lower)->SetMe::setX(int)(20)'', which is of non-aggregate type `void'' Would somebody explain how to remove this error. I want to retain the style of concatenating callsThe keypoint in this is, that each function has to return somethingthat the next call can act on.If you want to doSetMe lower;lower.setX( 20 ).setY(30);then the partlower.setX( 20 )must evaluate to something that is usable as object suchthat setY can work on. Best you start withSetMe lower;SetMe someObj;someObj.setY( 30 );now replace someObj with lower.setX( 20 )lower.setX( 20 ).setY( 20 );from this it is clear that the expression "lower.SetX( 20 )" musthave the same type as someObj before. Thus SetX must return a SetMeobject. (For obvious reasons I do the same modification on SetX,SetY and doubleMe simultanously)(Note: the following is not quite what you want, but close.I come back to it in a minute)class SetMe{public:SetMe setX(int x) { _x = x; return *this; }SetMe setY(int y) { _y = y; return *this; }SetMe doubleMe(){_x *= 2;_y *= 2;return *this;}private:int _x;int _y;};Now every function returns a SetMe object. Thus whenlower.SetX( 20 )is evaluated, its result is a SetMe object, which can beused for the next call:lower.setX( 20 ).doubleMe().setY( 30 );But wait: What is the exact return type of each function?It is ''SetMe''. That means that for every return a copyof the object that was worked on is returned. Thus in theabove the object for which doubleMe is called, is *not*lower, but is an object which is an exact copy of lower rightafter the call to SetX has finished. If this is not what youwant, then the return type needs some slight modification.What we want is not a copy of the object, but the object itself.We do this by returning a reference to the object:class SetMe{public:SetMe& setX(int x) { _x = x; return *this; }SetMe& setY(int y) { _y = y; return *this; }SetMe& doubleMe(){_x *= 2;_y *= 2;return *this;}private:int _x;int _y;};Now the returned object of setX is the very same object thatwas used for the call to setX ( ''this'' is the C++ way to say''I'' or ''me''. The only thing is that ''this'' is a pointer type.So to say ''return me'' you have to dereference the pointer, hencereturn *this; )Inlower.setX( 20 ).setY( 30 );the call to setX now returns a reference to the object it wascalled with, which was ''lower''. Thus setY will again work on ''lower''and do its work.That''s the whole secret.--Karl Heinz Buchegger kb******@gascad.at"Karl Heinz Buchegger" <kb******@gascad.at>, haber iletisinde sunlariyazdi:42***************@gascad.at...<snip> lower.setX( 20 ).setY( 30 );You can even begin with the class contructor like this:SetMe().setX( 20 ).setY( 30 );// if you don''t need "lower" after this line of course,// say passing it finally into a function as a parameter. the call to setX now returns a reference to the object it was called with, which was ''lower''. Thus setY will again work on ''lower'' and do its work. That''s the whole secret. -- Karl Heinz Buchegger kb******@gascad.at 这篇关于连接呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 01:07