Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting C to C++: typdef generating error

Tags:

c++

c

typedef

I need to convert a .c file into .cpp file and I came across this declaration:

 typedef void handler_t(int);

 handler_t *Signal(int signum, handler_t *handler);

I included those two lines of code in the header file, and I add the actual function declaration function in the .cpp file.

 handler_t *Signal(int signum, handler_t *handler){ ...... } 

When I do this I get the error: "handler_t does not name a type".I have never worked before with typdef in C or C++, so can someone explain to me why I am getting an error?


My classes as requested:

 #ifndef A_H
 #define A_H

 class A
 {
     public:
       A();
       virtual ~A();

       typedef void handler_t(int);
       handler_t* Signal(int signum, handler_t *handler);

     protected:
     private:
 };

   #endif // A_H

/////////////

   #include "A.h"

   A::A()
   {
   }

   A::~A()
   {
   }

   handler_t* A::Signal(int signum, handler_t *handler) {

     ...........

    }

error:

    |694|error: ‘handler_t’ does not name a type|
like image 434
FranXh Avatar asked Feb 14 '26 01:02

FranXh


1 Answers

Try changing it to:

typedef void (*handler_t)(int);

and:

handler_t Signal(int signum, handler_t handler);

For reference check out signal() which does it like this:

#include <signal.h>

typedef void (*sighandler_t)(int);

sighandler_t signal(int signum, sighandler_t handler);

Actually, looking at your new code, I think you have to do this:

A::handler_t* A::Signal(int signum, A::handler_t *handler)

I think your new error "undefined reference to main" is unrelated to the question asked. See this post for some ideas.

like image 102
Jorge Israel Peña Avatar answered Feb 16 '26 13:02

Jorge Israel Peña