Connecting Tech Pros Worldwide Help | Site Map

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

Prakash Bande
Guest
 
Posts: n/a
#1: Jul 22 '05
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*>::iterator i = find(ii, xv.end(), xyz);

c:\program files\include\algorithm(43) : error C2679: binary '==' : no
operator defined which takes a right-hand operand of type 'const class
std::basic_string<char,struct std::char_traits<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(&x1);
xx x2("some2");
vector<xx*>::iterator ii = xv.begin();
string xyz = "some2";
if (*ii == xyz )
{
}
while (ii != xv.end())
{
vector<xx*>::iterator i = find(ii, xv.end(), xyz);
xx *f = *i;
ii = i;
ii++;
}
}
tom_usenet
Guest
 
Posts: n/a
#2: Jul 22 '05

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


On 19 Aug 2004 00:56:10 -0700, prakash.bande@altair.com (Prakash
Bande) wrote:
[color=blue]
>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*>::iterator i = find(ii, xv.end(), xyz);
>
>c:\program files\include\algorithm(43) : error C2679: binary '==' : no
>operator defined which takes a right-hand operand of type 'const class
>std::basic_string<char,struct std::char_traits<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>[/color]

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
zvesko
Guest
 
Posts: n/a
#3: Jul 22 '05

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



"Prakash Bande" <prakash.bande@altair.com> wrote in message
news:dd3e7aa8.0408182356.636b8d8e@posting.google.c om...[color=blue]
> 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*>::iterator i = find(ii, xv.end(), xyz);
>
> c:\program files\include\algorithm(43) : error C2679: binary '==' : no
> operator defined which takes a right-hand operand of type 'const class
> std::basic_string<char,struct std::char_traits<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;[/color]

type of (st == obj->ab) is bool so

return (st == obj->ab) ;

is enough
[color=blue]
> }
>
> int main(int argc, char* argv[])
> {
> printf("Hello World!\n");
> vector<xx*> xv;
> xx x1("some1");
> xv.push_back(&x1);
> xx x2("some2");[/color]

I think you forgot to add

xv.push_back(&x2);
[color=blue]
> vector<xx*>::iterator ii = xv.begin();
> string xyz = "some2";
> if (*ii == xyz )
> {
> }
> while (ii != xv.end())
> {
> vector<xx*>::iterator i = find(ii, xv.end(), xyz);
> xx *f = *i;
> ii = i;
> ii++;
> }
> }[/color]

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_function<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*>::iterator i = find_if(ii, xv.end(), bind2nd(xx_equal(),
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


zvesko
Guest
 
Posts: n/a
#4: Jul 22 '05

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



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


Closed Thread


Similar C / C++ bytes