友好“>>"有自己的课 [英] Friending '>>' with own class
问题描述
我有一个以下课程,我与cout
作了朋友,现在我正尝试与cin
做朋友,但是我遇到了一个错误...有人可以帮助我,或者告诉我我做了什么错了吗?
I have the following class, which I friended with cout
and now I'm trying to friend it with cin
but I get an error... Could anyone help me, or tell me what I've done wrong?
错误:
c:\ mingw \ bin ../lib/gcc/mingw32/4.6.1/include/c ++/bits/stl_algo.h:2215:4:错误:将'const RAngle'传递为'this'参数int RAngle :: operator<(RAngle)'丢弃限定词[-fpermissive]
c:\mingw\bin../lib/gcc/mingw32/4.6.1/include/c++/bits/stl_algo.h:2215:4: error: passing 'const RAngle' as 'this' argument of 'int RAngle::operator<(RAngle)' discards qualifiers [-fpermissive]
类RAngle
:
class RAngle
{
private:
int *x,*y,*l;
public:
int solution,prec;
RAngle(){
this->x = 0;
this->y = 0;
this->l = 0;
}
RAngle(int i,int j,int k){
this->x = &i;
this->y = &j;
this->l = &k;
}
friend istream& operator >>( istream& is, RAngle &ra)
{
is >> ra->x;
is >> ra->y;
is >> ra->l;
return is ;
}
}
推荐答案
没有足够的代码来回答您的问题.但是从错误中我会告诉你,您的int RAngle::operator<(RAngle)
没有定义为const方法,而是在只有const的地方使用它.
There is not enough code to answer your question. But from error I would say you, that your int RAngle::operator<(RAngle)
is not defined as const method and you use it somewhere, where you have only const.
此外,使operator<
或其他比较运算符返回int不太可行,因为这可能会引起误解.此类运算符应返回bool
.
Also, it's not very good practive to make operator<
or other comparison operators return int, because this may lead to misunderstanding. Such operators should return bool
.
所以,会有这样的东西bool RAngle::operator<(const RAngle& other) const { /*...*/ }
.在此处和此处.
So, there sould be something like this bool RAngle::operator<(const RAngle& other) const { /*...*/ }
. This topic is covered here and here.
更新该代码是完全陌生的.为什么使用指向int
的指针?为什么将某些数据设为私有?构造函数RAngle(int i,int j,int k)
将无法正常工作.
Update This code is completely strange. Why use pointers to int
? Why make some data private? Constructor RAngle(int i,int j,int k)
will not work as you suppose.
这篇关于友好“>>"有自己的课的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!