“没有匹配函数用于调用...未解析的重载函数类型” [英] "No matching function for call to... unresolved overloaded function type"

查看:841
本文介绍了“没有匹配函数用于调用...未解析的重载函数类型”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个库,用户可以在实例级修改一个函数的行为,并仍然可以访问该实例的成员。此帖子是此主题的延续,后者是这一个。 Praetorian 建议我使用std :: function / bind来执行。不幸的是,我有两个错误:



Pb#1:错误:没有匹配函数调用'Child1 :: BindIt()' / p>

Pb#2:错误:没有匹配'operator ='(操作数类型是'std :: function'和'std :: _ Bind_helper) ;),Child1 ,const std :: _ placeholder< 1>> :: type {aka std :: _ Bind(Child1 ,std :: _ Placeholder& }')



我不明白如何解决这些问题。

  #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&)){
父< 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
}
void BindIt(T(* ptr_in)(const T&)){
父< 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<父级< lrtType> *>对象;

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 +++++



我更新了代码,但仍然有问题#2



Pb#2:错误:不匹配'operator ='(操作数类型是'std :: function'和'std :: _ Bind_helper)(const double&),Child1 ,const std :: _ Placeholder&类型{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 ::占位符:: _ 1); //这里的问题
}
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 ::占位符:: _ 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<父级< lrtType> *>对象;

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;
}


解决方案

是一个解决方案工作。但是有一些代码的重复,我害怕我不能避免它...

  #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<父级< lrtType> *>对象;

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;
}


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. Unfortunately, I have two errors:

Pb #1: error: no matching function for call to ‘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&)>}’)

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

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&)>}’)

#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;
}

这篇关于“没有匹配函数用于调用...未解析的重载函数类型”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆