468,720 Members | 1,633 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

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 1625
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by red floyd | last post: by
3 posts views Thread by ES Kim | last post: by
11 posts views Thread by franklini | last post: by
4 posts views Thread by dzikus | last post: by
reply views Thread by Andrew Maclean | last post: by
1 post views Thread by PS | last post: by
8 posts views Thread by Noah Roberts | last post: by
2 posts views Thread by benben | last post: by
4 posts views Thread by responsible | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.