473,739 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL::find does unable to call overloaded operator==

Hi,
I have bool operator == (xx* obj, const string st). I have declared it
as friend of class xx. I am now able to do this:
xx ox;
string st;
if (&ox == st)
{
}
But, when I have a vector<xx*> and use stl find
string xyz;
vector<xx*>::it erator i = find(ii, xv.end(), xyz);

c:\program files\include\a lgorithm(43) : error C2679: binary '==' : no
operator defined which takes a right-hand operand of type 'const class
std::basic_stri ng<char,struct std::char_trait s<char>,class
std::allocator< char> >' (or there is no acceptab
le conversion)

Here is a code snippet:

#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream.h>
using namespace std;
class xx;
bool operator == (xx* obj, const string st);
#include <algorithm>

class xx
{
friend bool operator == (xx* obj, const string st);
private:
string ab;
int i;
public:
xx(string a);
};

xx::xx(string a)
{
ab = a;
}
bool operator == (xx* obj, const string st)
{
if (st == obj->ab)
return true;
return false;
}

int main(int argc, char* argv[])
{
printf("Hello World!\n");
vector<xx*> xv;
xx x1("some1");
xv.push_back(&x 1);
xx x2("some2");
vector<xx*>::it erator ii = xv.begin();
string xyz = "some2";
if (*ii == xyz )
{
}
while (ii != xv.end())
{
vector<xx*>::it erator i = find(ii, xv.end(), xyz);
xx *f = *i;
ii = i;
ii++;
}
}
Jul 22 '05 #1
3 4440
On 19 Aug 2004 00:56:10 -0700, pr***********@a ltair.com (Prakash
Bande) wrote:
Hi,
I have bool operator == (xx* obj, const string st). I have declared it
as friend of class xx. I am now able to do this:
xx ox;
string st;
if (&ox == st)
{
}
But, when I have a vector<xx*> and use stl find
string xyz;
vector<xx*>::i terator i = find(ii, xv.end(), xyz);

c:\program files\include\a lgorithm(43) : error C2679: binary '==' : no
operator defined which takes a right-hand operand of type 'const class
std::basic_str ing<char,struct std::char_trait s<char>,class
std::allocator <char> >' (or there is no acceptab
le conversion)

Here is a code snippet:

#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream.h>


That should be
#include <iostream>
(many latest version compilers, including MSVC7.1 don't even have
<iostream.h>, and it is non-standard).
With that one change, it compiles and links fine (on VC7.1, for one).
I think your compiler doesn't implement argument dependent lookup, so
it doesn't find your globally declared operator, but instead only
finds the various operator==s declared in std. You may want to
upgrade.

Tom
Jul 22 '05 #2

"Prakash Bande" <pr***********@ altair.com> wrote in message
news:dd******** *************** ***@posting.goo gle.com...
Hi,
I have bool operator == (xx* obj, const string st). I have declared it
as friend of class xx. I am now able to do this:
xx ox;
string st;
if (&ox == st)
{
}
But, when I have a vector<xx*> and use stl find
string xyz;
vector<xx*>::it erator i = find(ii, xv.end(), xyz);

c:\program files\include\a lgorithm(43) : error C2679: binary '==' : no
operator defined which takes a right-hand operand of type 'const class
std::basic_stri ng<char,struct std::char_trait s<char>,class
std::allocator< char> >' (or there is no acceptab
le conversion)

Here is a code snippet:

#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream.h>
using namespace std;
class xx;
bool operator == (xx* obj, const string st);
#include <algorithm>

class xx
{
friend bool operator == (xx* obj, const string st);
private:
string ab;
int i;
public:
xx(string a);
};

xx::xx(string a)
{
ab = a;
}
bool operator == (xx* obj, const string st)
{
if (st == obj->ab)
return true;
return false;
type of (st == obj->ab) is bool so

return (st == obj->ab) ;

is enough
}

int main(int argc, char* argv[])
{
printf("Hello World!\n");
vector<xx*> xv;
xx x1("some1");
xv.push_back(&x 1);
xx x2("some2");
I think you forgot to add

xv.push_back(&x 2);
vector<xx*>::it erator ii = xv.begin();
string xyz = "some2";
if (*ii == xyz )
{
}
while (ii != xv.end())
{
vector<xx*>::it erator i = find(ii, xv.end(), xyz);
xx *f = *i;
ii = i;
ii++;
}
}


Very interesting, it compiles without a problem on VC7.1 but fails on VC6,
which I assume you're using.
No idea why, and it is not restricted to find. VC6 refuses to compile when
you have global operator ==
where the first argument is not a struct/class.

for example

bool operator == (int i, const string &){ return false; }

int ints[] = { 1, 2, 3, 4 };
size_t cnt = count(ints, ints+4, string("aaa"));

fails, whereas

bool operator == (const string &, int i){ return false; }

string strings[] = { "a", "b", "c", "d" };
size_t cnt = count(strings, strings+4, 5);

compiles

To go back to your problem... this works:

instead of operator== use binary predicate

struct xx_equal : public ::std::binary_f unction<xx*, ::std::string,
bool>
{
bool operator()(xx * obj, const ::std::string & st) const
{
/*1*/ return (st == obj->ab);

************
VC6 doesn't complain if you use your original operator == here
************
return obj == st;
}
};

then use find_if instead of find

#include <functional> // for bind2nd

vector<xx*>::it erator i = find_if(ii, xv.end(), bind2nd(xx_equa l(),
xyz));

you can declare xx_equal a friend of xx or even better declare accessor
function in xx for ab like

// inside xx
const string & get_ab() const { return ab; }

and instead of /*1*/ use

return (st == obj->get_ab());
VZ
Jul 22 '05 #3

This seems to work ok too.

move your operator== inside std namespace

//forward declare operator ==
namespace std { bool operator == (xx* obj, const string st); }

// to declare operator == as a friend
friend bool std::operator== (xx* obj, const string st);

// to define operator ==
namespace std
{
bool operator == (xx* obj, const string st){ return (st == obj->ab) ; }
}

VZ
Jul 22 '05 #4

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

Similar topics

4
2724
by: Tom | last post by:
Hi, I need some help with stl maps. I am using a std::map that is sorted in a very special way. Therefore I am using my own sort function. That works fine. But this is only half the problem. Additionally "map.find()" should use a different comparison function. My map is like: map < myString, Whatever>
16
2705
by: forester | last post by:
lets say its common situation when object have subobjects in container and receives callbacks from contained items. and object want to move objects in containers on signal(callback). iterator is needed for container modifications, item cannot know its iterator, at least its not easy to do so and i think cannot be done type-safe avoiding virtual functions and stuff. 'this' pointer from items is a freeby :>. the problem is, std...
9
28730
by: Henning Hasemann | last post by:
I'm using a stl-priority queue and want - find out if a certain item is contained in the queue - to be able iterate over all items without having to pop() them, order does not matter. I couldnt find methods for these which suprises me as both should be easily solvable with access to the underlying vector. Did I just miss these methods? Can I somehow access the vector to get . begin() .end() and .find()?
10
7529
by: Christian Chrismann | last post by:
Hi, I've a question on the STL find algorithm: My code: int main( void ) { vector< ClassA* myVector; ClassA *ptrElement1 = ...;
0
1671
by: PaperPilot | last post by:
While trying to copy data into the vector I get the following error messages: stl_algobase.h:247: error: no match for 'operator*' in '*__result' stl_algobase.h:249: error: no match for 'operator++' in '++__result' __result is the vector's OutputIterator the ecode segement that gives me this problem is: typedef std::vector< float > floatV;
0
8969
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
8788
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9335
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...
0
9208
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
8210
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
6053
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();...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
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.