0

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?

4
  • The only sensible way to solve this is to create a type alias for your function pointer. I believe it is possible without one, but the syntax is tricky. Commented Dec 14, 2022 at 18:45
  • I've tried with type alias. It was " typedef void( * return_func)(const dpp::slashcommand_t &event)". It gave the error: "typedef does not define a type". By the way @tkausi, it is an operator overloading. Commented Dec 14, 2022 at 18:49
  • std::function (part of modern c++ for a while now) might save you some trouble Commented Dec 14, 2022 at 18:58
  • 1
    @PierreBaret -- indeed, but at the cost of some overhead. This is not it's primary use. Commented Dec 14, 2022 at 18:58

1 Answer 1

12

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.