overload < operator, get a warning C4717



 DEVELOP > c-Plus-Plus > overload < operator, get a warning C4717

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

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



  Page 1 of 1

1

 


Related Articles
Re: For god's sake how do I get rid of this warning!
HOWTO: Get Warning on implicit type casts that may lead to data loss
How can I get rid of "Warning C4307: integral constant overflow"
The easiest way to get free cash!
A GREAT way to get FREE cash !!
In std C++,How to get the Current time?
Get Program Files folder name
Noob to C++, trying to get syntax, etc.
get free wallpapers and hot pictures[absolutely free]
better precision from ctime? (was Re: Get rid of a (float) cast in timer class)
get values from data file to integer variables..
Great Topic! Do You Want to List All the Files of a Directory? Get the BOOST FileSytem Now! It Works With Your C++ Compiler and It Is FREE!
is it possible to get index of object in map ? (linux, gcc)
Is it possible to get a variable's type and size in code?
How to get some C++ proficiency
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER