0

There is an header file with a struct having a function pointer as one of the members. I am trying to pass a private function from one of the derived abstract class to the function pointer in the struct. How can I do that? 1. I do not want a class header. I want only the struct in the header. 2. Instantiating abstract classes also requires implementaion of virtual methods. Can that be avoided?

//Channel_U.h
namespace MFW
{

class Channel_U : public Channel
{
public:

tOData theOutData;

private:
void ProcessFuncInput(tAnalogChannel * pAC);
void Process_Null(tAnalogChannel * pAC);
};
}
//table.h

namespace MFW
{
typedef void (*tAnalogProcessFunc)(t_AnalogChannel * pAC);


typedef struct _tableEntry_
{
  int      gain;
  void (*processFunc)(t_AnalogChannel *);
  tAnalogProcessFunc procFunc;
}table

static const table configTable[] =
{
 .processFunc    = Process_Null, //HERE
 .gain           = 0,
 .procFunc       = ProcessFuncInput//HERE
};
}

I tried instantiating the class object and callign the function which led to errors:

'Process_Null' was not declared in this scope
'ProcessFuncInput' was not declared in this scope

Alex Krish
  • 93
  • 1
  • 13
  • 1
    Slightly unrelated to your problem, but bointer to member functions are *not* the same as pointers to non-member functions. For a pointer to member function you need an actual object to call the member function on. – Some programmer dude Apr 11 '17 at 11:41
  • reopened as the error does not match the dupe problem. Leaving a link here as it will be a problem once you get the first error fixed: http://stackoverflow.com/questions/18731707/why-does-c11-not-support-designated-initializer-list-as-c99 – NathanOliver Apr 11 '17 at 11:46
  • Is `Channel` your abstract class? You don't have an any pure virtual functions in the code you have posted. Are all the `_table_entry_`s populated from `Channel` subclass instances? – Caleth Apr 11 '17 at 12:45
  • The compiler is right, those functions are not declared. It works if I declare them and change the syntax to table{ 0, Process_Null, ProcessFuncInput } But I'm not quite sure how to use Channel_U::ProcessFuncInput in a global object, as that doesn't make sense to me. Have you considered adding the channel instance to the table? – Kenny Ostrom Apr 11 '17 at 12:50
  • @Caleth Yes, `Channel` is an abstract class. Yes there are many pure virtual functions which I have not listed. No virtual functions are filled in the struct. And yes, the functions `Process_Null`, `ProcessFuncInput` are functions from Channel sublasses (`Channel_U` ) which are passed on to the struct. – Alex Krish Apr 11 '17 at 13:07
  • @KennyOstrom The functions are already declared in the class `Channel_U`. Or do you mean it to be re-declared in the file `table.h`? Trying to instantiate `Channel` class in `table.h` required implementation of pure virtual methods which I dont want to as there are many. – Alex Krish Apr 11 '17 at 13:13
  • No, Channel_U::Process_Null is a different type entirely, as mentioned in the first comment. Google "c++ pointer to member function". You could change it to class static, in which case it can be a globally callable function. Otherwise you'd be calling it with an undefined this pointer, and should expect that to be unpredictable. Oh, and if you do make it class static, you still have to refer to it by its actual name, Channel_U::Process_Null – Kenny Ostrom Apr 11 '17 at 14:10

0 Answers0