Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #70876

Re: invalid use of non-static member function

From Manfred <noname@add.invalid>
Newsgroups comp.lang.c++
Subject Re: invalid use of non-static member function
Date 2019-12-31 17:22 +0100
Organization Aioe.org NNTP Server
Message-ID <qufsjm$13p8$1@gioia.aioe.org> (permalink)
References <61205cc8-816c-437b-8128-4fd0d7458fdd@googlegroups.com> <quemlp$ggv$1@dont-email.me>

Show all headers | View raw


On 12/31/2019 6:34 AM, Alf P. Steinbach wrote:
> On 31.12.2019 05:01, Andrew Z wrote:
>> hello,
>>   im attempting to simplify the C code for arduino/idf framework.
>>   the setup_task function worked well until i introduced the Zap 
>> class. And now it is failing with :
>> [code]
>>
>> rc/main.cpp: In function 'void setup()':
>> src/main.cpp:112:115: error: invalid use of non-static member function
>>    setup_task(outlet.OutletToggleTask, (char*)"Outlet Task", 
>> eventGroup, outlet.OutletCallback, outlet.GetCycleTicks);
>>                                                                                                                     
>> ^
>> In file included from src/main.cpp:19:0:
>>
>> [/code]
>>
>> I appreciate the guidance and any help since I clearly don't 
>> understand what is wrong. I found a similar thread here 
>> (https://forum.arduino.cc/index.php?topic=524167.0). But im not sure 
>> why there is a need to create such a complex hierarchy...
>>
>> simplified code:
>> [code]
>> main.cpp
>> #include <Arduino.h>
>> ...
>> #include <zap.h>
>> #define pdMS_TO_TICKS( xTimeInMs ) (  ( TickType_t ) ( xTimeInMs ) * ( 
>> configTICK_RATE_HZ  / ( TickType_t ) 1000 ) )
>> const int TIME_BIT = BIT0 ;
>> #define DATA_PIN  4
>> #define CHANNEL   1
>>
>>
>> EventGroupHandle_t eventGroup;
>>
>> void setup_task(    void (*DeviceTask)(void*), char* TaskName, 
>> EventGroupHandle_t eventGroup,
>>                      void (*Callback_Fn)(TimerHandle_t), TickType_t 
>> cycle_ticks ) {};
> 
> Use `const char*` for C string that's not going to be changed.
> 
> It's UB to cast a literal to `char*` and the modify it.
> 
> 
>> void setup(){
>>
>>     eventGroup = xEventGroupCreate(); //check if NULL NOT returned.
>>     Zap outlet(DATA_PIN, CHANNEL , 20000); //  make a new outlet 
>> instance with
>>     setup_task(outlet.OutletToggleTask, (char*)"Outlet Task", 
>> eventGroup, outlet.OutletCallback,
> 
> OutletToggleTask is a non-static member function.
> 
> As a non-static member function it takes a hidden `this` parameter, 
> which might not be passed in the same way as other parameters. Therefore 
> you can't convert it to pointer-to-free-function in any portable way.
> 
> Additionally the construct `outlet.OutletToggleTask` is a callable 
> expression that in itself has no type. It's silly in a very serious way: 
> the lack of a type for it is a core language deficiency that stands in 
> the way of easily using C++ for e.g. GUI code, or other code that 
> requires object oriented callbacks. C# has delegates for this, and the 
> C++ standard library has `std::function` and `std::bind`, but it really 
> requires core language support for ease of use.
> 
> One easy practical solution to your problem is to simply declare the 
> function `static` in the class, or make it a free function.
> 
> 
> [snip]
> 
> - Alf

There's a second problem, which is might be the one detected by the 
compiler, although the above is definitely an error too.
The last argument of setup_task is expected to be an object of type 
TickType_t, but a pointer to member function is passed instead: 
"outlet.GetCycleTicks" should be "outlet.GetCycleTicks()"

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

invalid use of non-static member function Andrew Z <formisc@gmail.com> - 2019-12-30 20:01 -0800
  Re: invalid use of non-static member function "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-12-31 06:34 +0100
    Re: invalid use of non-static member function Manfred <noname@add.invalid> - 2019-12-31 17:22 +0100
      Re: invalid use of non-static member function Andrew Z <formisc@gmail.com> - 2019-12-31 22:32 -0800
        Re: invalid use of non-static member function Manfred <noname@add.invalid> - 2020-01-01 18:15 +0100
          Re: invalid use of non-static member function Andrew Z <formisc@gmail.com> - 2020-01-01 18:49 -0800
    Re: invalid use of non-static member function Andrew Z <formisc@gmail.com> - 2019-12-31 23:03 -0800
      Re: invalid use of non-static member function "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2020-01-01 22:48 +0100
        Re: invalid use of non-static member function Andrew Z <formisc@gmail.com> - 2020-01-15 07:35 -0800

csiph-web