[comp.lang.c++] G++ question

king1@husc10.harvard.edu (Gary King) (05/07/91)

HI,

I'm using the following C++ class to interface to the interval
timer and the SIGALRM signal in UNIX.  g++ doesn't mind the header
file and it doesn't mind the constructor, but when it gets
to int ticker::sethandler( void (*f)(int) ) it complains:


	ticker.cc: 'f' undeclared, outside of functions
	ticker.cc: parse error before ')'


Can anyone explain this and what to do to make it work.


Thanks in advance.... (truncated code appears below):


/*
**	ticker class
**			This class is an interface to the UNIX
**			alarm system call.  Only one is allowed at a
**			time so we make everything in it static.
*/

#include	<signal.h>

class ticker {
public:
	ticker( int, void (*f) (int) );
	~ticker();

	// user interface
	...
	static int	sethandler( void (*) (int) );

private:
	static void	set_ticker( int = 0 );
	static	int	ticks;			// how often to go off
	static  void	(*handler)(int);	// what to do when we ring
};

::::::::::::::::::
ticker.cc
::::::::::::::::::

#include	<sys/time.h>
#include	<stdlib.h>
#include	"ticker.h"

void syserr( char * );

ticker :: ticker( int ticktock, void (*func) (int) ) {
	ticker :: ticks = ticktock;
	ticker :: handler = func;
	signal( SIGALRM, ticker :: handler );
}

...
...
...

static int	
ticker :: sethandler( void (*xll)(int) ) {
	signal( SIGALRM, (ticker :: handler = func ) );
	return 0;
}

hitz@csi.uottawa.ca (Martin Hitz) (05/08/91)

In article <1991May6.183253.880@husc3.harvard.edu> king1@husc10.harvard.edu (Gary King) writes:
> [...] when [g++] gets
>to int ticker::sethandler( void (*f)(int) ) it complains:
>	ticker.cc: 'f' undeclared, outside of functions
>	ticker.cc: parse error before ')'
>
>class ticker {
>public:
>	ticker( int, void (*f) (int) );
>	static int	sethandler( void (*) (int) );
>	// ...
>};
>
>ticker :: ticker( int ticktock, void (*func) (int) ) { /* ... */ }
>
>static int	
>ticker :: sethandler( void (*xll)(int) ) {/* ... */ }

Playing around with your program, I realized that g++ isn't able
to parse the function parameter definition when it appears as the
first parameter [It accepts both, the declaration and the definition of
ticker(), though.  But if you specify a parameter name at the declaration of
sethandler(), the above error message appears already there]. This is
probably a bug, which can be worked around using

typedef void (*fptr) (int);

class ticker {
	// ...
        ticker( int, fptr );
        static int sethandler( fptr );
};
ticker :: ticker( int ticktock, fptr func ) { /*...*/ }

static
 int      
ticker :: sethandler( fptr func ) { /*...*/ }
 

Martin Hitz@csi.uottawa.ca

hitz@csi.uottawa.ca (Martin Hitz) (05/08/91)

In article <1991May6.183253.880@husc3.harvard.edu> king1@husc10.harvard.edu (Gary King) writes:
> [...] when [g++] gets
>to int ticker::sethandler( void (*f)(int) ) it complains:
>       ticker.cc: 'f' undeclared, outside of functions
>       ticker.cc: parse error before ')'
>
>class ticker {
>public:
>       ticker( int, void (*f) (int) );
>       static int      sethandler( void (*) (int) );
>       // ...
>};
>
>ticker :: ticker( int ticktock, void (*func) (int) ) { /* ... */ }
>
>static int     
>ticker :: sethandler( void (*xll)(int) ) {/* ... */ }

Playing around with your program, I realized that g++ isn't able
to parse the function parameter definition when it appears as the
first parameter [It accepts both, the declaration and the definition of
ticker(), though.  But if you specify a parameter name at the declaration of
sethandler(), the above error message appears already there]. This is
probably a bug, which can be worked around using

typedef void (*fptr) (int);

class ticker {
        // ...
        ticker( int, fptr );
        static int sethandler( fptr );
};
ticker :: ticker( int ticktock, fptr func ) { /*...*/ }

static int ticker :: sethandler( fptr func ) { /*...*/ }
 

Martin Hitz@csi.uottawa.ca