473,762 Members | 6,675 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(cons t Windowinfo &wnd);
friend bool operator <(const Windowinfo &left,const
Windowinfo &right)
{
if(left.wndId<r ight.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 3436
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(cons t Windowinfo &wnd);
friend bool operator <(const Windowinfo &left,const
Windowinfo &right)
{
if(left.wndId<r ight.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<righ t.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(cons t 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<r ight.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2529
by: Shailesh | last post by:
Hi! I want to overload << operator so that it can print an arry defined in MyClass.My problem is that I want to print only a no of elements NOT all the elements in the array and this no of elements depends upon user input.The code below describes this in detail: class MyClass { public: unsigned short x;
7
4340
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; } This overload work but I have a curiosty If I try to approch to the name or to the surname or to the dateofbirth in this way i receive error: ostream & operator<<(ostream &out, Person &p){ out<<p.name<<" "<<p.surname<<", "<<p.dateofbirth<<endl;
17
2510
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: public static bool operator !=(MyType x, MyType y) { return !(x == y); } That way the == operator handles everything, and extra comparing logic isn't
3
1657
by: Suresh Tri | last post by:
Hi all, I was trying to overload '<' operator for (varchar,varchar). But in the function which handles the comparision I want to use the previous '<' operator.. but it is going into a recursion. My simplified code looks like : create or replace function orastringcmp (varchar, varchar) returns boolean as 'declare
2
4165
by: pasa_1 | last post by:
The following code results in Segmentation fault ========================= #include <iostream> using namespace std; class abc{
7
14924
by: glen | last post by:
Hi. I'm using GCC 4.1.1, which I mention since I don't know if this is a compiler issue, or me not understanding some subtlety in the standard. The code below compiles fine under vc++, but I'm having trouble using gcc. It's just a templated container output. /** outputs elements of a vector in the form '{el1,el2...elEnd}'
4
1817
by: nomad5000 | last post by:
Hello! I'm trying to overload the << operator but it just won't work my code is the following: the student.h file #include <string>
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.