467,225 Members | 1,311 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,225 developers. It's quick & easy.

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
  • viewed: 3042
Share:
5 Replies
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 this site, you agree to our Privacy Policy and Terms of Use.