完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
亲爱的各位,我最近一直在使用和声2.01,为长时间等待的C++特性做一个尝试。到目前为止,除了已知的app.h生成的代码问题,我没有遇到任何麻烦。但是以前版本的C HARMony(我猜只有2.01和C)中的某些东西现在是个问题。它与类成员函数指针有关,与我得到的函数指针不同。下面的错误:MyClass.hppMyClass.cppTo有相同的签名,我已经尝试将C++回调声明为静态成员函数,但它不起作用。因为我使用这个来避免阻塞读/写UART操作,所以我不应该是第一个遇到这种情况的。你有工作吗?请注意,我已经检查了Harmony 2.01(纯C)中的驱动程序/usart/usart_echo示例(并与Harmony以前的版本一起使用)。
以上来自于百度翻译 以下为原文 Dear all, I have been using Harmony 2.01 lately in order to give a try for the long awaited C++ feature. Until now, except the known app.h generated code issue, I did not have any trouble. But something that was working in C with previous versions of Harmony (and I guess 2.01 as well with C only) is now an issue. It is related with class member function pointer different from a function pointer as I get the following error: ../src/MyClass.cpp: In member function 'void MyClass::init(SYS_MODULE_INDEX)': ../src/MyClass.cpp:19:89: error: cannot convert MyClass::bufferEventHandler' from type 'void (MyClass::)(DRV_USART_BUFFER_EVENT, DRV_USART_BUFFER_HANDLE, uintptr_t) {aka void (MyClass::)(DRV_USART_BUFFER_EVENT, unsigned int, unsigned int)}' to type 'DRV_USART_BUFFER_EVENT_HANDLER {aka void (*)(DRV_USART_BUFFER_EVENT, unsigned int, unsigned int)}' DRV_USART_BufferEventHandlerSet(drvUsartHandle, bufferEventHandler, (uintptr_t)1); ^ MyClass.hpp void bufferEventHandler(DRV_USART_BUFFER_EVENT event, DRV_USART_BUFFER_HANDLE bufferHandle, uintptr_t context); MyClass.cpp void MyClass::bufferEventHandler(DRV_USART_BUFFER_EVENT event, DRV_USART_BUFFER_HANDLE bufferHandle, uintptr_t context) { switch (event) { case DRV_USART_BUFFER_EVENT_COMPLETE: // Handle the completed buffer. break; case DRV_USART_BUFFER_EVENT_ERROR: default: // Handle error. break; } } [...] void MyClass::init(SYS_MODULE_INDEX drvUsartIndex) { drvUsartHandle = DRV_USART_Open(drvUsartIndex, DRV_IO_INTENT_WRITE); if (DRV_HANDLE_INVALID != drvUsartHandle) { // We register our event handling callback function DRV_USART_BufferEventHandlerSet(drvUsartHandle, bufferEventHandler, (uintptr_t)1); // We can begin the RESET sequence state = RESET_STATE; } else { // Unable to open the driver // May be the driver is not initialized or the initialization // is not complete. } } To have the same signature, I have tried declaring the C++ callback as a static member function but it did not work. Since I am using this to avoid blocking read/write USART operations, I should not be the first to encounter this. Have you any workaround? Please note that I have checked the driver/usart/usart_echo sample in Harmony 2.01 (pure C) (and use it with previous version of Harmony). Thanks for your help, Regards |
|
相关推荐
4个回答
|
|
首先,我希望MyClass.hpp声明一个实际的C++类,并且BuffReEvEnDeLand()是这个类的一个成员。第二,在传递一个类成员的指针时,比如BuffReEvEnDeDrEnter(),你不能只传递一个与C函数相同的指针。这是因为每一个C++类成员函数除了指定的参数之外还接收一个附加的参数——“this”指针。“this”指针允许代码访问作为类成员的变量。解决这个问题的方法是将bufferEventHandler()声明为静态类成员。然后它没有一个“this”指针。因此,您的MyClass.hpp将包含以下内容:类MyClass{public:MyClass();//构造函数静态无效缓冲区EventHandler(DRV_USART_BUFFER_EVENT事件,DRV_USART_BUFFER_HANDLE缓冲区Handle,uintptr_t上下文);};注意,我在void bufferEventHandler()之前添加了“.”;这与C函数的静态不一样,它创建静态链接。相反,这更像是其他编程语言中所谓的“类方法”。它是应用于类的所有实例而不是特定实例的函数。在C++中,像这样的静态类方法有一个“this”指针。因此,它将无法访问特定实例的变量。上下文指针。当您设置回调并将指向该静态类方法的函数指针传递给Harmony时,您将把指向该类的特定实例的指针转换为(uintptr_t),并将其作为上下文指针.DRV_USART_SetUpWhateverHandler(blah,blah,MyClass::bufferEventHandler,(uintptr_t)&I这听起来比实际情况要复杂得多。如果您对此感到不舒服,那么将bufferEventHandler()设置为正常函数(非类成员),并从那里调用类。
以上来自于百度翻译 以下为原文 First of all, I hope MyClass.hpp declares an actual C++ class and that bufferEventHandler() is a member of this class. Second, when passing a pointer to a class member, like bufferEventHandler() in this case, you cannot just pass a pointer the same as with a C function. This is because every C++ class member function receives an additional argument in addition to the ones you specify -- the "this" pointer. The "this" pointer is what allows your code to access variables that are members of the class. The way to get around that is to declare bufferEventHandler() as a static class member. Then it does not have a "this" pointer. So your MyClass.hpp would contain something like: class MyClass { public: MyClass(); // constructor static void bufferEventHandler(DRV_USART_BUFFER_EVENT event, DRV_USART_BUFFER_HANDLE bufferHandle, uintptr_t context); }; Note I added "static" before void bufferEventHandler(). This is NOT the same as static for C functions, which creates static linkage. Rather, this is more like what's called a "class method" in other programming languages. It is a function that applies to ALL instances of the class, not to a specific instance. In C++, static class methods like this one have no "this" pointer. Therefore it will NOT be able to access the variables of a specific instance. How do you get around this? The context pointer. When you set up your callback and pass to Harmony a function pointer to this static class method, you'll cast a pointer to the specific instance of the class to (uintptr_t) and pass that as the context pointer. DRV_USART_SetUpWhateverHandler(blah, blah, MyClass::bufferEventHandler, (uintptr_t) &InstanceOfMyClass); This sounds more complicated than it really is. If you're uncomfortable with it, make bufferEventHandler() a normal function (non class member) and call into your class from there. |
|
|
|
你好,谢谢你的详细而详尽的回答:我有个主意,但不能想出一个“好”的方法来解决它。我对静态类成员的尝试被其他问题分散了注意力。我不能用硬件测试(我正在度假),但是这个编译(并且应该工作)。我正在把可能有用的源代码.MyCuff.HPPyCyrase.CPP
以上来自于百度翻译 以下为原文 Hello, thank you for your detailed and well-explained answer: I had the main idea but could not figure out a 'good' way to solve it. My attempt with static class member was distracted by other issues. I can not test with hardware (I am on vacation), but this compiles (and should work). I am putting the source that might be helpful. MyClass.hpp /** @Function bufferEventHandler() @Summary Callback when a queue buffer USART transfer have finished. @Description When DRV_USART_BufferAddRead or DRV_USART_BufferAddWrite functions have finished sending their buffer, this callback is called on indicate status of the transfer. A static class member is used to match the signature of the C callback. @Precondition DRV_USART_Open must have been called to obtain a valid opened device handle. @Parameters @param event The possible events that can result from a buffer add request. @param bufferHandle The handle identifying the buffer to which the event relates. @param context The pointer to the instance that registered the event handling function. */ static void bufferEventHandler(DRV_USART_BUFFER_EVENT event, DRV_USART_BUFFER_HANDLE bufferHandle, uintptr_t context); MyClass.cpp void MyClass::init(HardwareSettings hardwareSettings) { this->hardwareSettings = hardwareSettings; // Try to open the USART driver and returns a handle to it drvUsartHandle = DRV_USART_Open(this->hardwareSettings.usartDriverIndex, DRV_IO_INTENT_READWRITE); if (DRV_HANDLE_INVALID != drvUsartHandle) { // We register our event handling callback function DRV_USART_BufferEventHandlerSet(drvUsartHandle, bufferEventHandler, (uintptr_t) this); // We can begin the RESET sequence state = SEND_RESET; } else { // Unable to open the driver // May be the driver is not initialized or the initialization // is not complete. } } void MyClass::bufferEventHandler(DRV_USART_BUFFER_EVENT event, DRV_USART_BUFFER_HANDLE bufferHandle, uintptr_t context) { // Explicitly cast to a pointer of this class MyClass* myInstance = (MyClass*) context; switch (event) { case DRV_USART_BUFFER_EVENT_COMPLETE: // Handle the completed buffer myInstance->state = SEND_EOT; break; case DRV_USART_BUFFER_EVENT_ERROR: default: // Handle error, reset the line myInstance->init(myInstance->hardwareSettings); break; } } |
|
|
|
|
|
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
5189 浏览 9 评论
2009 浏览 8 评论
1933 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3181 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2232 浏览 5 评论
743浏览 1评论
629浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
512浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
642浏览 0评论
538浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-30 11:17 , Processed in 1.251685 second(s), Total 86, Slave 67 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号