[英]“No matching function for call to… unresolved overloaded function type”
I am trying to create a library where the user can modify the behaviour of a function on an instance-level and still manage to access the members of this instance. 我正在尝试创建一个库,用户可以在该库中在实例级别上修改功能的行为,并且仍然设法访问该实例的成员。 This post is the continuation of this thread which is the continuation of this one .
这篇文章是延续这个线程是延续这一个 。 Praetorian suggested me to use std::function/bind to do that.
Praetorian 建议我使用std :: function / bind来做到这一点。 Unfortunately, I have two errors:
不幸的是,我有两个错误:
Pb #1: error: no matching function for call to 'Child1::BindIt()' 铅#1: 错误:没有匹配的函数来调用'Child1 :: BindIt()'
Pb #2: error: no match for 'operator=' (operand types are 'std::function' and 'std::_Bind_helper)(const double&), Child1 , const std::_Placeholder<1>&>::type {aka std::_Bind(Child1 , std::_Placeholder<1>))(const double&)>}') 铅#2: 错误:'operator ='不匹配(操作数类型为'std :: function'和'std :: _ Bind_helper)(const double&),Child1 ,const std :: __ Placeholder <1>&> :: type {aka std :: _ Bind(Child1 ,std :: _ Placeholder <1>))(const double&)>}'))
I don't understand how to solve these problems. 我不明白如何解决这些问题。
#include <iostream>
#include <vector>
#include <cstdlib>
#include <functional>
using namespace std;
template<typename T,typename D> T fcn_default(const D &obj, const T &phit){
return 3.2 + phit;
}
template<typename T> class Parent{
public:
Parent() {}
T do_something(const T &phit){
return this->fcn_ptr(phit);
}
std::function<T(const T&)> fcn_ptr;
};
template<typename T> class Child1 : public Parent<T>{
public:
Child1() {
BindIt( &fcn_default< T , Child1<T> > ); // Pb #1: here
}
void BindIt(T (*ptr_in)(const T &) ){
Parent<T>::fcn_ptr = std::bind(&ptr_in, this, std::placeholders::_1); // Pb #2: here
}
};
template<typename T> class Child2 : public Parent<T>{
public:
Child2() {
BindIt( &fcn_default< T , Child2<T> > ); // Pb #1: and here
}
void BindIt(T (*ptr_in)(const T &) ){
Parent<T>::fcn_ptr = std::bind(&ptr_in, this, std::placeholders::_1);
}
T param2;
};
template<typename T> T fcn_mod1 (const Child1<T> &obj, const T &phit){
return 1.2 + phit;
}
template<typename T> T fcn_mod2 (const Child2<T> &obj, const T &phit){
return 2.2 + phit + obj.param2*0.001;
}
typedef double lrtType;
int main(){
std::vector< Parent<lrtType> * > objects;
Child1<lrtType> *test11 = new Child1<lrtType>();
objects.push_back(test11);
Child1<lrtType> *test12 = new Child1<lrtType>();
//test12->BindIt(&fcn_mod1);
objects.push_back(test12);
Child2<lrtType> *test2 = new Child2<lrtType>();
//test2->BindIt(&fcn_mod2);
test2->param2 = 4;
objects.push_back(test2);
for (size_t i = 0; i < objects.size(); ++i) {
std::cout << objects[i]->do_something(2) << std::endl;
}
std::cout << "test" << std::endl;
}
+++++ UPDATE +++++ +++++更新+++++
I updated the code but I still have problem #2 我更新了代码,但仍然有问题2
Pb #2: error: no match for 'operator=' (operand types are 'std::function' and 'std::_Bind_helper)(const double&), Child1 , const std::_Placeholder<1>&>::type {aka std::_Bind(Child1 , std::_Placeholder<1>))(const double&)>}') 铅#2: 错误:'operator ='不匹配(操作数类型为'std :: function'和'std :: _ Bind_helper)(const double&),Child1 ,const std :: __ Placeholder <1>&> :: type {aka std :: _ Bind(Child1 ,std :: _ Placeholder <1>))(const double&)>}'))
#include <iostream>
#include <vector>
#include <cstdlib>
#include <functional>
using namespace std;
template<typename T,typename D> T fcn_default(const D &obj, const T &phit){
return 3.2 + phit;
}
template<typename T> class Parent{
public:
Parent() {}
virtual T do_something (const T &phit) const = 0;
};
template<typename T> class Child1 : public Parent<T>{
public:
Child1() {
Child1<T>::BindIt( &fcn_default< T , Child1<T> > );
}
void BindIt(T (*ptr_in)(const Child1 &, const T &) ){
fcn_ptr = std::bind(&ptr_in, this, std::placeholders::_1); // Problem here
}
std::function<T(const Child1 &, const T &)> fcn_ptr;
T do_something(const T &phit) const {
return this->fcn_ptr(*this,phit);
}
};
template<typename T> class Child2 : public Parent<T>{
public:
Child2() {
Child2<T>::BindIt( &fcn_default< T , Child2<T> > );
}
void BindIt(T (*ptr_in)(const Child2 &, const T &) ){
fcn_ptr = std::bind(&ptr_in, this, std::placeholders::_1); // And here
}
std::function<T(const Child2 &, const T &)> fcn_ptr;
T do_something(const T &phit) const {
return this->fcn_ptr(*this,phit);
}
T param2;
};
template<typename T> T fcn_mod1 (const Child1<T> &obj, const T &phit){
return 1.2 + phit;
}
template<typename T> T fcn_mod2 (const Child2<T> &obj, const T &phit){
return 2.2 + phit + obj.param2*0.001;
}
typedef double lrtType;
int main(){
std::vector< Parent<lrtType> * > objects;
Child1<lrtType> *test11 = new Child1<lrtType>();
objects.push_back(test11);
Child1<lrtType> *test12 = new Child1<lrtType>();
//test12->BindIt(&fcn_mod1);
objects.push_back(test12);
Child2<lrtType> *test2 = new Child2<lrtType>();
//test2->BindIt(&fcn_mod2);
test2->param2 = 4;
objects.push_back(test2);
for (size_t i = 0; i < objects.size(); ++i) {
std::cout << objects[i]->do_something(2) << std::endl;
}
std::cout << "test" << std::endl;
}
If somebody is interested, below is a solution that works. 如果有人感兴趣,下面是一个可行的解决方案。 There is however important duplication of code and I fear I can not avoid it...
但是有重要的代码重复,我担心我无法避免。
#include <iostream>
#include <vector>
#include <cstdlib>
#include <functional>
using namespace std;
template<typename T,typename D> T fcn_default(const D &obj, const T &phit){
return 3.2 + phit;
}
template<typename T> class Parent{
public:
Parent() {}
virtual T do_something (const T &phit) const = 0;
};
template<typename T> class Child1 : public Parent<T>{
public:
Child1() {
fcn_ptr = &fcn_default< T , Child1<T> >;
}
std::function<T(const Child1<T> &, const T &)> fcn_ptr;
T do_something(const T &phit) const {
return (*this).fcn_ptr(*this,phit);
}
};
template<typename T> class Child2 : public Parent<T>{
public:
Child2() {
fcn_ptr = &fcn_default< T , Child2<T> >;
}
std::function<T(const Child2<T> &, const T &)> fcn_ptr;
T do_something(const T &phit) const {
return (*this).fcn_ptr(*this,phit);
}
T param2;
};
template<typename T> T fcn_mod1 (const Child1<T> &obj, const T &phit){
return 1.2 + phit;
}
template<typename T> T fcn_mod2 (const Child2<T> &obj, const T &phit){
return 2.2 + phit + obj.param2*0.001;
}
typedef double lrtType;
int main(){
std::vector< Parent<lrtType> * > objects;
Child1<lrtType> *test11 = new Child1<lrtType>();
objects.push_back(test11);
Child1<lrtType> *test12 = new Child1<lrtType>();
test12->fcn_ptr = &fcn_mod1<lrtType>;
objects.push_back(test12);
Child2<lrtType> *test2 = new Child2<lrtType>();
test2->fcn_ptr = &fcn_mod2<lrtType>;
test2->param2 = 4;
objects.push_back(test2);
for (size_t i = 0; i < objects.size(); ++i) {
std::cout << objects[i]->do_something(2) << std::endl;
}
std::cout << "test" << std::endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.