Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Returning a Function Pointer in C++

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 for how I can resolve this error?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading