473,398 Members | 2,380 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,398 software developers and data experts.

problem with bind2nd

Hi,

I am having the following question (see code below)

the class Data is declared as
class Data
{
public :
// rest of class
};

class Base : public binary_function<string, Data, bool>
{
public :
virtual bool operator() (const string& line, Data d) const = 0;
};

class Derived_1 : public Base
{
bool operator()(const string& line, Data d)
{
// do your stuff
return true or false;
}
};

class Derived_2 : public Base
{
bool operator()(const string& line, Data d)
{
// do your stuff
return true or false;
}
};

in some other class which has a vector dataLines I want to perform a find
on the vector. My problem is that sometimes I need to call the operator()
on the derived_1 class and another time I need to call operator() on the
Derived_2 class. The find function has a pointer to Derived_1 or Derived_2
class. But what do I put as argument for the bind2nd class

bind2nd( // What do i put here // , data)

I tried bind2nd( c() , data) but is gives me the error below

error C2064: term does not evaluate to a function taking 0 arguments

void MyClass::find(Compare* c, Data data)
{
vector<string>::iteraor result;
result = find_if(dataLines.begin(), dataLines.end(), bind2nd( // What do
i put here // , data));

// rest of code

}

Does anyone know how to fix this

thanks a lot

John
Jun 27 '08 #1
3 1795
On May 17, 7:06*pm, "Bruintje Beer" <m...@knoware.nlwrote:
Hi,

I am having the following question (see code below)

the class Data is declared as
class Data
{
public :
* * // rest of class

};

class Base : public binary_function<string, Data, bool>
{
public :
* * virtual bool operator() (const string& line, Data d) const = 0;

};

class Derived_1 : public Base
{
* * bool operator()(const string& line, Data d)
const
* * {
* * * * // do your stuff
* * * * return true or false;
* * }

};

class Derived_2 : public Base
{
* * bool operator()(const string& line, Data d)
const
* * {
* * * * // do your stuff
* * * * return true or false;
* * }

};

in some other class which has a vector dataLines I want to perform a find
on the vector. My problem is that sometimes I need to call the operator()
on the derived_1 class and another time I need to call operator() on the
Derived_2 class. The find function has a pointer to Derived_1 or Derived_2
class. But what do I put as argument for the bind2nd class

bind2nd( // What do i put here // , data)

I tried bind2nd( c() , data) but is gives me the error below

error C2064: term does not evaluate to a function taking 0 arguments

void MyClass::find(Compare* c, Data data)
{
* * vector<string>::iteraor result;
iterator
* * result = find_if(dataLines.begin(), dataLines.end(), bind2nd( //What do
i put here // , data));

* * // rest of code

*}
Well, maybe std::bind2nd(Derived_1(), data);
Don't understand what's 'Compare' here. Wild guess: 'Base'

But keep in mind that STL functions *never* (AFAIK) take a functor by
reference (nor pointer),
so polymorphism is not satisfied.

If you mean 'Base' by 'Compare',
then bind2nd(*c, data) will cause a compile error,
saying 'Base' is an abstract class, can't be instantiated.

Jun 27 '08 #2
In article <48**********************@news.tiscali.nl>,
"Bruintje Beer" <me@knoware.nlwrote:
Hi,

I am having the following question (see code below)

the class Data is declared as
class Data
{
public :
// rest of class
};

class Base : public binary_function<string, Data, bool>
{
public :
virtual bool operator() (const string& line, Data d) const = 0;
};

class Derived_1 : public Base
{
bool operator()(const string& line, Data d)
{
// do your stuff
return true or false;
}
};

class Derived_2 : public Base
{
bool operator()(const string& line, Data d)
{
// do your stuff
return true or false;
}
};

in some other class which has a vector dataLines I want to perform a find
on the vector. My problem is that sometimes I need to call the operator()
on the derived_1 class and another time I need to call operator() on the
Derived_2 class. The find function has a pointer to Derived_1 or Derived_2
class. But what do I put as argument for the bind2nd class

bind2nd( // What do i put here // , data)

I tried bind2nd( c() , data) but is gives me the error below

error C2064: term does not evaluate to a function taking 0 arguments

void MyClass::find(Compare* c, Data data)
{
vector<string>::iteraor result;
result = find_if(dataLines.begin(), dataLines.end(), bind2nd( // What do
i put here // , data));

// rest of code

}

Does anyone know how to fix this

thanks a lot
Frankly, it doesn't make a lot of sense to have an op() as pure virtual.
How are you supposed to call it? How would you call it if you were
writing the loop yourself?

class Data { };

class Derived_1 : public binary_function<string, Data, bool>
{
public:
bool operator()(const string& line, Data d) const
{
// do your stuff
return true or false;
}
};

class Derived_2 : public binary_function<string, Data, bool>
{
public:
bool operator()(const string& line, Data d) const
{
// do your stuff
return true or false;
}
};

class MyClass {
vector<stringdataLines;
public: // <-- whatever access restriction you want
template < typename Fn >
void find(Fn f, Data data)
{
vector<string>::iterator result =
find_if(dataLines.begin(), dataLines.end(), bind2nd(f, data));
}
};

int main()
{
MyClass c;
Data d;
c.find(Derived_1(), d);
c.find(Derived_2(), d);
}
Jun 27 '08 #3

"Daniel T." <da******@earthlink.netschreef in bericht
news:da****************************@earthlink.vsrv-sjc.supernews.net...
In article <48**********************@news.tiscali.nl>,
"Bruintje Beer" <me@knoware.nlwrote:
>Hi,

I am having the following question (see code below)

the class Data is declared as
class Data
{
public :
// rest of class
};

class Base : public binary_function<string, Data, bool>
{
public :
virtual bool operator() (const string& line, Data d) const = 0;
};

class Derived_1 : public Base
{
bool operator()(const string& line, Data d)
{
// do your stuff
return true or false;
}
};

class Derived_2 : public Base
{
bool operator()(const string& line, Data d)
{
// do your stuff
return true or false;
}
};

in some other class which has a vector dataLines I want to perform a find
on the vector. My problem is that sometimes I need to call the operator()
on the derived_1 class and another time I need to call operator() on the
Derived_2 class. The find function has a pointer to Derived_1 or
Derived_2
class. But what do I put as argument for the bind2nd class

bind2nd( // What do i put here // , data)

I tried bind2nd( c() , data) but is gives me the error below

error C2064: term does not evaluate to a function taking 0 arguments

void MyClass::find(Compare* c, Data data)
{
vector<string>::iteraor result;
result = find_if(dataLines.begin(), dataLines.end(), bind2nd( // What
do
i put here // , data));

// rest of code

}

Does anyone know how to fix this

thanks a lot

Frankly, it doesn't make a lot of sense to have an op() as pure virtual.
How are you supposed to call it? How would you call it if you were
writing the loop yourself?

class Data { };

class Derived_1 : public binary_function<string, Data, bool>
{
public:
bool operator()(const string& line, Data d) const
{
// do your stuff
return true or false;
}
};

class Derived_2 : public binary_function<string, Data, bool>
{
public:
bool operator()(const string& line, Data d) const
{
// do your stuff
return true or false;
}
};

class MyClass {
vector<stringdataLines;
public: // <-- whatever access restriction you want
template < typename Fn >
void find(Fn f, Data data)
{
vector<string>::iterator result =
find_if(dataLines.begin(), dataLines.end(), bind2nd(f, data));
}
};

int main()
{
MyClass c;
Data d;
c.find(Derived_1(), d);
c.find(Derived_2(), d);
}
Hi,

I do not understand right now but this is exactly what i needed. Thanks a
lot.

John

Jun 27 '08 #4

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

Similar topics

1
by: red floyd | last post by:
Both gcc 3.3.1 and MSVC 7.1 complain about the second for_each() call in the following code. Why can't I use bind2nd for a functor with a non-const reference as the second parameter? I have to...
3
by: ES Kim | last post by:
Here's a simple code: #include <vector> #include <algorithm> #include <functional> using namespace std; struct S { void f(int) const { }
11
by: franklini | last post by:
hello people, just wanted to say thanks again for the help in the past. i have a new problem which am wondering if any body can help me with. i have written this abtract class shape and its...
4
by: dzikus | last post by:
Why the following code does not compile? If I change the function argument to value (not reference) everything is ok. Is it possible to modify such code that it will compile with reference argument...
0
by: Andrew Maclean | last post by:
I guess this problem can be distilled down to: How do I search through a string, find the first matching substring, replace it, and continue through the string doing this. Can replace_if() be used...
1
by: PS | last post by:
Hi, I wrote the following code to try using bind2nd. It compiles and works fine with g++ but a Sun compiler tells me : "test_bind2nd.cpp", line 21: Error: Cannot cast from...
8
by: Noah Roberts | last post by:
#include <vector> #include <algorithm> #include <functional> class X { int x; public: X(int i) : x(i) {} bool eq(const X & other) const { return x == other.x; }
2
by: benben | last post by:
I wrote the following lines: #include <locale> #include <functional> #include <algorithm> int main() { using namespace std; bind2nd(ptr_fun(tolower<char>), locale());
4
by: responsible | last post by:
Hi, Digging through the STL source code, i found this gem.. template <class _Operation, class _Tp> inline binder2nd<_Operation> bind2nd(const _Operation& __fn, const _Tp& __x) { typedef...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.