473,689 Members | 2,940 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()(cons t string& line, Data d)
{
// do your stuff
return true or false;
}
};

class Derived_2 : public Base
{
bool operator()(cons t 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(C ompare* c, Data data)
{
vector<string>: :iteraor result;
result = find_if(dataLin es.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 1812
On May 17, 7:06*pm, "Bruintje Beer" <m...@knoware.n lwrote:
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()(cons t string& line, Data d)
const
* * {
* * * * // do your stuff
* * * * return true or false;
* * }

};

class Derived_2 : public Base
{
* * bool operator()(cons t 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(C ompare* c, Data data)
{
* * vector<string>: :iteraor result;
iterator
* * result = find_if(dataLin es.begin(), dataLines.end() , bind2nd( //What do
i put here // , data));

* * // rest of code

*}
Well, maybe std::bind2nd(De rived_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.nlw rote:
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()(cons t string& line, Data d)
{
// do your stuff
return true or false;
}
};

class Derived_2 : public Base
{
bool operator()(cons t 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(C ompare* c, Data data)
{
vector<string>: :iteraor result;
result = find_if(dataLin es.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()(cons t string& line, Data d) const
{
// do your stuff
return true or false;
}
};

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

class MyClass {
vector<stringda taLines;
public: // <-- whatever access restriction you want
template < typename Fn >
void find(Fn f, Data data)
{
vector<string>: :iterator result =
find_if(dataLin es.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******@earth link.netschreef in bericht
news:da******** *************** *****@earthlink .vsrv-sjc.supernews.n et...
In article <48************ **********@news .tiscali.nl>,
"Bruintje Beer" <me@knoware.nlw rote:
>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()(cons t string& line, Data d)
{
// do your stuff
return true or false;
}
};

class Derived_2 : public Base
{
bool operator()(cons t 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(C ompare* c, Data data)
{
vector<string>: :iteraor result;
result = find_if(dataLin es.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()(cons t string& line, Data d) const
{
// do your stuff
return true or false;
}
};

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

class MyClass {
vector<stringda taLines;
public: // <-- whatever access restriction you want
template < typename Fn >
void find(Fn f, Data data)
{
vector<string>: :iterator result =
find_if(dataLin es.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
3375
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 use kludge it by using something like KludgeFunctor(). --------------------------- #include <vector> #include <algorithm> #include <functional>
3
2668
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
2801
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 derived class circle, rectangle and triangle. i have also written this method for each shape object called 'draw_all_seq_inside2' which takes two values. i want to call this method using for_each statement in another method but it doesnt seem to...
4
4899
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 type? Best regards Dominik #include <string> #include <list> #include <algorithm>
0
420
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 to do this? Here is a concrete example: If I have a string with sequences of CRLF and possibly just CR's, is there a simple way of replacing the CRLF characters with a LF? If you look at the function below is it possible to use a replace_if...
1
3031
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 std::binder2nd<std::greater<int>> to bool(*)(const int&). Could you tell me if there's something wrong with the code itself? Thanks, PS
8
4495
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
483
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
1837
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 typename _Operation::second_argument_type _Arg2_type; return binder2nd<_Operation>(__fn, _Arg2_type(__x)); }
0
8594
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
8528
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
8947
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
8783
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
8786
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
7621
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...
1
6454
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4321
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4547
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.