<unresolved overloaded function type> wenn Sie versuchen, um eine aggregierte Methode eines Objekts zu seiner Klasse Methode

Ich habe einige Probleme kompilieren meines Codes.
Ich habe die folgende Struktur:

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

Wenn ich versuche zu kompilieren, es gibt:

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

Was ist hier falsch?

p/s/AnotherClass * obj_; so bleiben soll, denn ich Schreibe eine Funktion, die große Bibliothek und kann es nicht ändern.

-------------- funktionierende version von Benjamin -------

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

Ich danke Ihnen allen sehr für die Hilfe!

Bitte schreiben Sie einen minimalen beleidigen Beispiel. Der Fehler ist nicht verursacht durch den code, den Sie uns zeigen.
Bitte poste realen code (cut down, um ein minimal-Beispiel), keine pseudo-code.
Sieht aus wie Sie erklärt funcAnother als member-Funktion. Wenn Sie machen es zu einem static Klasse Funktion sollte es funktionieren. Natürlich, wenn es Referenzen, es wird nicht funktionieren und Sie brauchen einen anderen Ansatz.
Reproduzieren Sie das problem in einem minimal-Programm, dann poste den kompletten code.
es gibt keine member-Funktion void run() im class MyClass. Auch die AnotherClass definiert werden(oder wenigstens vorwärts deklariert) werden, bevor MyClass

InformationsquelleAutor Denis | 2011-08-10

Schreibe einen Kommentar