简体   繁体   English

将函数指针从 Arduino 传递给 C++

[英]Passing function pointer from Arduino to C++

This question is about C++, but it interact with Arduino, and since I am not a software developer, I would be happy to get some advice.这个问题是关于 C++,但它与 Arduino 交互,由于我不是软件开发人员,我很乐意得到一些建议。

In Arduino, a user creates an instance of a class and set its delegate method:在 Arduino 中,用户创建一个类的实例并设置其委托方法:

Arduino main program:

    //instance
    Main interpreter;
    //set delegate
      interpreter.setDelegate(intepreterDelegate);
    //delegate function
         float intepreterDelegate(char *arg){return 3;}

Main class, is then creating an instance of another class, called Sub , and so : Main类然后创建另一个类的实例,称为Sub ,因此:

Sub - > send delegate to Main-> send delegate to Arduino

The Main class does successfully get the delegate message from the Sub with: Main确实通过以下方式成功地从 Sub获取了委托消息:

Main.h

  //being called from the sub
   static float fromSubDelegate(char *arg){

        // *Here I am trying to push forward this delegate out to the user on arduino
        Main. b;   
        float result = (b.*b.fpAction)(arg);
        return  result;


     };

    float (Main.::*fpAction)(char*) = 0 ;
    void setDelegate( float(*fp)(char*));

The problem is here- on Main.cpp where I set the delegate问题出在我设置委托的Main.cpp

//arduino set this delegate of the main class .cpp
void Main.::setDelegate( float(*fp)(char*))
{

    fpAction = fp; // *gives error because fpAction is a member function

}

I have provided all the data I have.我已经提供了我拥有的所有数据。 I am not a C++ programmer, hence I might be doing something wrong.我不是 C++ 程序员,因此我可能做错了什么。

This isn't really what you asked in the other question.这不是你在另一个问题中问的。 you have a mismatch between what you (seem to) do in the adruino code and what you want in the C++ code.您(似乎)在 adruino 代码中执行的操作与您在 C++ 代码中想要执行的操作之间存在不匹配。

you cannot change your delegate from a non-member function to a member-function and you define a member function (ie the delegate) outside of the class that it uses.您不能将委托从非成员函数更改为成员函数,并且您不能在它使用的类之外​​定义成员函数(即委托)。

what you're trying doesn't make sense currently.你正在尝试的东西目前没有意义。 your arduino defines an instance你的 arduino 定义了一个实例

Main interpreter;

it then defines and sets the delegate (ok so far) IN the interpreter instance.然后它在解释器实例中定义和设置委托(到目前为止还可以)。

but in fromSubDelegate() you create a new instance b.但是在 fromSubDelegate() 中,您创建了一个新实例 b。 whichever delegate you may have set in the interpreter instance won't be set in the b instance, unless the function pointer is static, but in that case you don't even need the fromSubDelegate() function, you can just call the function pointer directly.您在解释器实例中设置的任何委托都不会在 b 实例中设置,除非函数指针是静态的,但在这种情况下,您甚至不需要 fromSubDelegate() 函数,您只需调用函数指针即可直接。

You seem to have a wrong idea of how things work.你似乎对事情的运作方式有一个错误的想法。 The thing to take home is that if you have a non-static member function of a class, there is a hidden extra parameter, which you can use from within the function as 'this'.要带回家的是,如果您有一个类的非静态成员函数,则有一个隐藏的额外参数,您可以在函数中将其用作“this”。 so a member function you define as所以你定义的成员函数

class X
{
    void func(int i);
}

isn't the same as不一样

void func (int i);

defined outside of a class.在类之外定义。 It is really more similar to void真的更像void

func(X* this, int i); 

although that's semantically 'correct', it isn't syntactically.尽管这在语义上是“正确的”,但在语法上却并非如此。

Thanks to the help of oreubens , I found a solution - just to pass directly from the main to the sub, a pointer for the function , so :感谢oreubens的帮助,我找到了一个解决方案 - 只是直接从 main 传递到 sub,函数的指针,所以:

void Main::setDelegate( float(*fp)(char*))
{
    fpAction=fp;
    sub->setDelegate(fpAction);



}

works great.效果很好。 thanks again .再次感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM