Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Syntax error in function prototype accepting function as parameter

syntax syntax error

  • Please log in to reply
3 replies to this topic

#1 reugot

reugot

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 12 November 2010 - 01:41 PM

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.
  • 0

#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 12 November 2010 - 02:57 PM

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.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#3 reugot

reugot

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 12 November 2010 - 05:40 PM

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.
  • 0

#4 reugot

reugot

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 12 November 2010 - 05:47 PM

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.
  • 0





Also tagged with one or more of these keywords: syntax, syntax error

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download