472,128 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,128 software developers and data experts.

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.

Jun 28 '07 #1
5 3282
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.
Jun 28 '07 #2
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
Jun 28 '07 #3
Erik Wikström <Er***********@telia.comwrote:
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....a516bccf0ac287

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 28 '07 #4
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]
Jun 28 '07 #5
On 2007-06-28 23:06, Marcus Kwok wrote:
Erik Wikström <Er***********@telia.comwrote:
>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....a516bccf0ac287
Cool.

--
Erik Wikström
Jun 28 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Piotre Ugrumov | last post: by
17 posts views Thread by Chris | last post: by
3 posts views Thread by Suresh Tri | last post: by
2 posts views Thread by pasa_1 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.