473,394 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

operator < stopped working

All were working well until I decided to add a copy constructor.
Please see the program. I am getting

"c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include
\functional(139) : error C2679: binary '<' : no operator found which
takes a right-hand operand of type 'const D' (or there is no
acceptable conversion)
"

Seems like I can't guarantee constant-ness anymore with the copy
constructor. How do I get around with this ?

// test-sets.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include <iostream>
#include <set>
#include <map>

using namespace std;

class D
{

private:
string a;

public:
//Constructor
D()
{
};
//Destructor
~D()
{
};
//Copy constructor
D(D& d)
{
};
std::string& getA()
{
return a;
}
bool operator < (D d) const
{
if(a.compare(d.getA()) <0){
return true;
}else {
return false;
}
}
};

int main()
{
typedef std::set<Ddsets;
D *d = new D();
dsets s;
s.insert(*d);
return 0;
}

Oct 2 '07 #1
1 1604
raan wrote:
All were working well until I decided to add a copy constructor.
Please see the program. I am getting

"c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include
\functional(139) : error C2679: binary '<' : no operator found which
takes a right-hand operand of type 'const D' (or there is no
acceptable conversion)
"

Seems like I can't guarantee constant-ness anymore with the copy
constructor. How do I get around with this ?

// test-sets.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include <iostream>
#include <set>
#include <map>

using namespace std;

class D
{

private:
string a;

public:
//Constructor
D()
{
};
Please drop the semicolons after the function bodies. They are
so ugly.
//Destructor
~D()
{
};
//Copy constructor
D(D& d)
The usual signature (the compiler-generated one's at least) is

D(D const& d)
{
};
std::string& getA()
{
return a;
}
There is also a need to have a const version of that function:

std::string const& getA() const
{
return a;
}

Don't let it throw you off that they have the same body, they are
different!
bool operator < (D d) const
The usual (idiomatic) way is to pass the operand by a reference
to cons:

bool operator < (D const& d) const
{
if(a.compare(d.getA()) <0){
return true;
}else {
return false;
}
}
};

int main()
{
typedef std::set<Ddsets;
WHY?
D *d = new D();
WHY?
dsets s;
s.insert(*d);
Eek!
return 0;
}
What you should write is

std::set<Ddsets;
dsets.insert(D());

Or at least

std::set<Ddsets;
D d;
dsets.insert(d);

Drop the habit of doing "new" all the time. Besides, you're
inconsistent. You didn't 'new' your 'dsets' object!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 2 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: tHatDudeUK | last post by:
My form action code to submit values to itself have stopped working using the code form action = <?=$_SERVER?> This code used to work My web host recently told me they enabled phpsuexec...
5
by: Juri | last post by:
Hi all, I have a question regarding the << operator. I wrote a simple queue class, which contains add(char), del() and empty() functions. They works fine. Now I want its output. If I write the...
13
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and...
4
by: Rock | last post by:
I'm in the process of writing this program for complex numbers and I use DevC++. My professor on the other hand compiles on Borland 5.5. So I ocasionally save and run my work on Borland to see if...
3
by: mensanator | last post by:
## Holy Mother of Pearl! ## ## >>> for i in range(10): ## for j in range(10): ## print '%4d' % (gmpy.mpz(i)*gmpy.mpz(j)), ## print ## ## ## ...
17
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks...
2
by: ryan_melville | last post by:
Hi, Should I put the operator<<() for my class (which is in a namespace) in the namespace or make it global? If I understand the lookup rules correctly: If I make it global, it may be hidden...
2
by: VK | last post by:
2.12 What does the future hold for ECMAScript? <old> The fourth edition of ECMAScript will provide new features like typed variables, and classes. More information can be found at:...
6
by: Rob McDonald | last post by:
I would like to force all the classes in my hierarchy to implement the << operator for testing purposes. My base class is a pure virtual class. I started out by working with operator...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...

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.