Home > Enterprise >  Returning a Function Pointer in C
Returning a Function Pointer in C

Time:12-15


Here is the code:

void (*)(const dpp::slashcommand_t &event) operator[](const std::string &index);

When I compile this, I get the following error message:

In file included from commands_controller.cpp:3,
                 from main.cpp:2:
commands_controller.h:28:12: error: expected unqualified-id before ‘)’ token
   28 |     void (*)(const dpp::slashcommand_t &event) operator[](const std::string &index);

Do you have any suggestion,that how could I resolve this error?

CodePudding user response:

When dealing with function pointers, use typdefs. The correct non-typedef syntax is:

void (*operator[](const std::string &index))(const dpp::slashcommand_t &event)

https://coliru.stacked-crooked.com/a/4eabda845ac15a54

Obviously, this is completely insane.

So instead, always use a typedef or alias:

using slash_fn_ptr = void (*)(const dpp::slashcommand_t &event);
slash_fn_ptr operator[](const std::string &index);

https://coliru.stacked-crooked.com/a/fd96b9918b1526b7

  • Related