【问题标题】:<unresolved overloaded function type> when trying to pass an aggregated object's method to its class method<未解析的重载函数类型> 尝试将聚合对象的方法传递给其类方法时
【发布时间】:2011-10-24 07:52:34
【问题描述】:

我在编译我的代码时遇到了一些问题。 我有以下结构:

#include <cstdlib>

using namespace std;

typedef double (*FuncType)(int );

class AnotherClass {
   public:
          AnotherClass() {};       
   double funcAnother(int i) {return i*1.0;}
};

class MyClass {
public:
         MyClass(AnotherClass & obj) { obj_ = &obj;};
    void compute(FuncType foo);
    void run();

    protected:
      AnotherClass * obj_;   /*pointer to obj. of another class */   
};

void MyClass::compute(FuncType foo) 
{
    int a=1;
    double b;
    b= foo(a);    
}

void MyClass::run()
{
     compute(obj_->funcAnother);
}

/*
 * 
 */
int main(int argc, char** argv) {
    AnotherClass a;
    MyClass b(a);
    b.run();    

    return 0;
}

当我尝试编译它时,它给出:

main.cpp:39:31: error: no matching function for call to ‘MyClass::compute(<unresolved overloaded function type>)’
main.cpp:30:6: note: candidate is: void MyClass::compute(double (*)(int))

这里有什么问题?

p/s/ AnotherClass * obj_; 应该保持这样,因为我在大库中编写了一些函数并且无法更改它。

-------------本杰明的工作版本--------

#include <cstdlib>

using namespace std;


class AnotherClass {
   public:
          AnotherClass() {};       
   double funcAnother(int i) {return i*1.0;}
};


struct Foo
{

    /*constructor*/
    Foo(AnotherClass & a) : a_(a) {};

    double operator()(int i) const
    {
        return a_.funcAnother(i);
    }          

    AnotherClass & a_;               
};


class MyClass {
public:
         MyClass(AnotherClass & obj) { obj_ = &obj;};

    template<typename FuncType>     
    void compute(FuncType foo);
    void run();

   protected:
      AnotherClass * obj_;   /*pointer to obj. of another class */   
};

template<typename FuncType>
void MyClass::compute(FuncType foo) 
{
    int a=1;
    double b;
    b= foo(a);    
}

void MyClass::run()
{
    Foo f(*obj_);
    compute(f);
}

/*
 * 
 */
int main(int argc, char** argv) {
    AnotherClass a;
    MyClass b(a);
    b.run();    

    return 0;
}

非常感谢大家的帮助!

【问题讨论】:

  • 请发布一个最小的违规示例。该错误不是由您向我们展示的代码引起的。
  • 请发布真实代码(简化为最小示例),而不是伪代码。
  • 看起来您将funcAnother 声明为成员函数。如果你把它设为static 类函数,它应该可以工作。当然,如果它引用它就行不通,你需要一种不同的方法。
  • 用最小的程序重现问题,然后贴出完整的真实代码。
  • class MyClass 中没有成员函数void run()。此外,AnotherClass 将在MyClass 之前定义(或至少向前声明)

标签: c++ function pointers call aggregation


【解决方案1】:

因为,

funcAnother(int i);

是一个成员函数,它传递一个隐含的this,然后原型与你的函数指针的类型不匹配。

成员函数指针的typedef应该是:

typedef double (AnotherClass::*funcPtr)(int);

Here 是您的代码的修改后的可编译版本。请检查内联的 cmets 以了解更改,另外我省略了其他详细信息,您可以添加。

【讨论】:

  • 那么我应该如何修改我的指针,使其符合我要调用的函数?
  • 这可能不是错误(但这仍然是所提供代码的错误)。错误来自尝试将重载的函数名称分配给函数指针。
  • @Denis:更新了答案。您需要执行以下操作:1. 发布 actual 示例代码,2. 发布 complete 示例,上面的更新解决了 一个 看到的错误在您的代码中,它不能解决您发布的错误,这可能是因为您的类中有不明确的重载函数并且您正在尝试使用它们。
  • 我更新了代码。也尝试了你的解决方案,但它并没有解决问题,仍然有“未解决的重载函数”错误。
  • @Denis:更新了答案,点击链接获取代码的可编译版本。
【解决方案2】:

以下函数类将匹配您的 FuncType 的签名:

struct Foo
{
    AnotherClass & a_;
    Foo(AnotherClass & a) a_(a) {}

    double operator()(int i) const
    {
        return a_.funcAnother(i);
    }
};

将 MyClass::compute 更改为模板,因此:

template<typename FuncType>
void MyClass::compute(FuncType foo) 
{
    int a=1;
    foo(a);
}

然后你可以这样调用run:

void MyClass::run()
{
    compute(Foo(*obj_));
}

如果您的编译器支持 lambdas(而且很有可能支持),那么您可以放弃函数类并像这样简单地定义 run:

void MyClass::run()
{
    auto f = [this](int i) {
        return obj_->funcAnother(i);
    };

    compute(f);
}

【讨论】:

    猜你喜欢
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多