| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"David" |
| Date: |
28 Jun 2007 02:55:24 PM |
| Object: |
overload < operator, get a warning C4717 |
Hi all,
I am trying to overload the < operator, but get warning
class Windowinfo
{
protected:
HWND wndhandle; //the window handle
int wndId; //the window Id
public:
Windowinfo();
Windowinfo(const Windowinfo &wnd);
friend bool operator <(const Windowinfo &left,const
Windowinfo &right)
{
if(left.wndId<right.wndId)
left<right;
else
right<left;
return true;
}
};
The warning: warning C4717: 'operator<' : recursive on all control
paths, function will cause runtime stack overflow.
I think maybe the overload function calles itself. how to solve it?
Thanks.
.
|
|
| User: "Stefan Naewe" |
|
| Title: Re: overload < operator, get a warning C4717 |
28 Jun 2007 03:04:55 PM |
|
|
David schrieb:
Hi all,
I am trying to overload the < operator, but get warning
class Windowinfo
{
protected:
HWND wndhandle; //the window handle
int wndId; //the window Id
public:
Windowinfo();
Windowinfo(const Windowinfo &wnd);
friend bool operator <(const Windowinfo &left,const
Windowinfo &right)
{
if(left.wndId<right.wndId)
left<right;
else
right<left;
return true;
}
};
The warning: warning C4717: 'operator<' : recursive on all control
paths, function will cause runtime stack overflow.
I think maybe the overload function calles itself. how to solve it?
Thanks.
I guess you simply want to do this in your operator< :
return left.wndId<right.wndId;
HTH
S.
.
|
|
|
|
| User: "=?ISO-8859-1?Q?Erik_Wikstr=F6m?=" |
|
| Title: Re: overload < operator, get a warning C4717 |
28 Jun 2007 03:53:23 PM |
|
|
On 2007-06-28 21:55, David wrote:
Hi all,
I am trying to overload the < operator, but get warning
class Windowinfo
{
protected:
HWND wndhandle; //the window handle
int wndId; //the window Id
public:
Windowinfo();
Windowinfo(const Windowinfo &wnd);
friend bool operator <(const Windowinfo &left,const
Windowinfo &right)
First of, this is the non-member version of the operator, you have to
define it outside of the class, or use the member version
bool operator<(const Windowinfo& right)
{
if(left.wndId<right.wndId)
left<right;
This one seems to be a comment rather than something you want executed.
What it does is to compare an object of type Windowinfo with another
object of the same type, and you compare them using the less than
operator, which just so happens to be the one you are in, which will
give you an infinite recursion.
else
right<left;
return true;
Same here.
}
};
The warning: warning C4717: 'operator<' : recursive on all control
paths, function will cause runtime stack overflow.
I think maybe the overload function calles itself. how to solve it?
Thanks.
As Stefan Naewe pointed out, you only have to do
return left.wndId < right.wndId;
--
Erik Wikström
.
|
|
|
| User: "Marcus Kwok" |
|
| Title: Re: overload < operator, get a warning C4717 |
28 Jun 2007 04:06:04 PM |
|
|
Erik Wikström <Erik-wikstrom@telia.com> wrote:
On 2007-06-28 21:55, David wrote:
class Windowinfo
{
public:
friend bool operator <(const Windowinfo &left,const
Windowinfo &right)
First of, this is the non-member version of the operator, you have to
define it outside of the class
Not necessarily; see the thread starting here:
http://groups.google.com/group/comp.lang.c++/browse_frm/thread/d926e30ed08dd8c8/66a516bccf0ac287
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
.
|
|
|
| User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=" |
|
| Title: Re: overload < operator, get a warning C4717 |
28 Jun 2007 04:18:44 PM |
|
|
On 2007-06-28 23:06, Marcus Kwok wrote:
Erik Wikström <Erik-wikstrom@telia.com> wrote:
On 2007-06-28 21:55, David wrote:
class Windowinfo
{
public:
friend bool operator <(const Windowinfo &left,const
Windowinfo &right)
First of, this is the non-member version of the operator, you have to
define it outside of the class
Not necessarily; see the thread starting here:
http://groups.google.com/group/comp.lang.c++/browse_frm/thread/d926e30ed08dd8c8/66a516bccf0ac287
Cool.
--
Erik Wikström
.
|
|
|
|
|
| User: "red floyd" |
|
| Title: Re: overload < operator, get a warning C4717 |
28 Jun 2007 04:09:00 PM |
|
|
Erik Wikström wrote:
On 2007-06-28 21:55, David wrote:
Hi all,
I am trying to overload the < operator, but get warning
class Windowinfo
{
[redacted]
friend bool operator <(const Windowinfo &left,const
Windowinfo &right)
First of, this is the non-member version of the operator, you have to
define it outside of the class, or use the member version
No, he can inline it. Note the friend declaration.
[remainder redacted]
.
|
|
|
|
|

|
Related Articles |
|
|