overload < operator, get a warning C4717

D

David

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.
 
S

Stefan Naewe

David said:
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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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;
 
R

red floyd

Erik said:
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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top