Jump to content

Syntax error in function prototype accepting function as parameter

- - - - -

  • Please log in to reply
3 replies to this topic

#1
reugot

reugot

    Newbie

  • Members
  • Pip
  • 3 posts
I'm trying to fix a function prototype for a function where I am passing another function as a parameter:
Function in file.cpp:

int runcmd(int(*func)(CMDArgs*), char *somevar) {

    CMDArgs* myparams = (CMDArgs *) malloc(sizeof(CMDArgs));

    ZeroMemory(myparams, sizeof(CMDArgs));

    HANDLE cmdthr = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, myparams, 0, NULL);

}

Prototype in file.h:

int runcmd(int(*func)(CMDArgs*), char *somevar);


When trying to compile, i get 3 errors:
2 undeclared identifiers: func and CMDArgs
1 syntax error : ')'

I can remove the undeclared identifiers by deleting them in the prototype, but I still get the syntax error. I have searched around for examples on doing this, as I wasn't sure how to pass functions as parameters before, but I haven't found anything showing why I'm having issues. All the errors pertain to that one line of code (which is line 1 of file.h). I haven't seen anything that would make me thing the issue is somewhere else. What am I missing/doing wrong? Also, why would I get the two identifier errors, when I have other function prototypes with variable names in them not causing errors. Thanks in advance for any help.

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Where do you have CMDArgs declared? Make sure you're including that before your function prototype. Also, I'd use a typedef, like so:

typedef int (*callback_func)(CMDArgs *);


int runcmd(callback_func func, char *somevar);


Makes it easier to read.
sudo rm -rf /

#3
reugot

reugot

    Newbie

  • Members
  • Pip
  • 3 posts
The structure is declared before. It's in a separate header file, but that file is #include'd first. This is my structure definition:

So, in structs.h

typedef struct {

	char * args;

	HANDLE outwr;

} CMDArgs;


In file.h
typedef int (*callback_func)(CMDArgs *);
and the previous function definition
the typedef gives me errors on CMDArgs (undeclared) and ')' syntax error
My function definition gives me the same errors.
The function itself is located in file.cpp and the includes go:
#include "structs.h"
#include "file.h"
Also, the function itself works. I don't know if it's because I did it correctly, or it's by chance, lol.
I just don't get why the function itself gives me no errors and works fine, but the prototype does.

#4
reugot

reugot

    Newbie

  • Members
  • Pip
  • 3 posts
Disregard the above post (I tried editing, but it wasn't working). I decided to explicitly include structs.h in file.h and that got it working. I was trying to avoid that...but it works so....Thanks for your help.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users