3

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|
FranXh
  • 4,481
  • 20
  • 59
  • 78
  • Please provide a complete demonstration, focusing entirely your problem. The demonstration should contain the entirety of two files: The header and the code file. The two files should contain only enough code to demonstrate the problem. – autistic Mar 17 '13 at 00:17
  • 2
    Since it's in the A class, you probably need to use A::handler_t – Dave Mar 17 '13 at 00:25
  • you could also refer this http://stackoverflow.com/questions/10801312/c-class-typedef-struct-does-not-name-a-type – Vbp Mar 17 '13 at 00:27
  • You mean like this? It generates an error: "undefined reference to main" and I am not using main at all: A::handler_t* A::Signal(int signum, A::handler_t *handler) { ........... } – FranXh Mar 17 '13 at 00:28
  • 2
    @FranXh Your new error is unrelated to your original question. If you are compiling an executable you need a `main()` function. – Code-Apprentice Mar 17 '13 at 00:37
  • Yes, sorry for the stupid question. Just realized that. @Dave I believe it works with the way you suggested. I am testing it now. – FranXh Mar 17 '13 at 00:54
  • 1
    OK great. If it's fixed you should accept Jorge's answer to close the question, since he also mentioned that. – Dave Mar 17 '13 at 00:56
  • No there is still a problem in my main class. I mentioned it also in the comment below – FranXh Mar 17 '13 at 01:03

2 Answers2

2

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.

Community
  • 1
  • 1
Jorge Israel Peña
  • 36,800
  • 16
  • 93
  • 123
  • 1
    If we assume this compiled as C but not as C++, then I don't think this is the problem. (also I hate typedefs that mask pointers) – Dave Mar 17 '13 at 00:26
  • isn't that he should have handler_t typedef first somewhere, and his typedef void handler_t(int); wants just to extend it? – 4pie0 Mar 17 '13 at 00:28
  • @Dave Isn't it a pointer to function? I think this is the only way you have to keep your sanity :) – Andrea Richiardi Mar 17 '13 at 00:29
  • @Kap I meant that `handler_t *handler` makes more sense than `sighandler_t handler` (where the latter has been typedefed as a function pointer whereas the former is just typedefed as a function). It's personal opinion, but I think that indirection levels should always be made clear. – Dave Mar 17 '13 at 00:33
  • @JorgeIsraelPeña regarding my new error: I am using codeblocks, so I do not see why that error should happen – FranXh Mar 17 '13 at 00:35
  • @FranXh Codeblocks isn't a compiler, it's an IDE. I imagine you're using gcc/mingw as the compiler itself (check the settings). My point is that I think the new error you're getting is no longer caused by the question you're asking. It should be it's own separate question, in other words. I think the changes you made has solved the question you asked here. – Jorge Israel Peña Mar 17 '13 at 00:37
  • Yes agreed, it was my mistake the second error. Sorry just ignore that – FranXh Mar 17 '13 at 00:54
  • 1
    @FranXh Hey man no worries, it happens to all of us. I was pointing it out mainly to make the problem clearer for you, I'm sure it would've been more confusing not knowing what was causing what and trying to find a common solution to both problems. – Jorge Israel Peña Mar 17 '13 at 01:06
0
handler_t* A::Signal(int signum, handler_t *handler) {

The problem is that the first mention of handler_t needs to be qualified with A::. Like this:

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

This is always the case when types defined inside a class are used as the return types of member functions. When you declare the function you're inside the class definition, so the nested typedef is known. And it's the same if you define the function inside the class definition. But once you step outside the class definition you have to tell the compiler where handler_t is defined. But the second mention of handler_t is okay, because at that point the compiler knows that it's compiling a member function of A, and it knows about the nested types in A.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • I think I understand this. But the problem is that I get this error when I try to call signal from my main function: 39|error: ‘Signal’ was not declared in this scope| – FranXh Mar 17 '13 at 01:02
  • @FranXh the method is part of a class, so you should be calling it through an A object. – Jorge Israel Peña Mar 17 '13 at 01:08
  • Ok, so I did this, where SIGINT it's just an integer and sigint_handler is a void function: A a; a.Signal(SIGINT, a.sigint_handler); The error this time is: 39|error: no matching function for call to ‘A::Signal(int, )’| – FranXh Mar 17 '13 at 01:15
  • @FranXh Of course you `#include`ed the file that defines the `class A` right? – Jorge Israel Peña Mar 17 '13 at 02:16
  • yes, I have many other function calls from A and they work fine in main. This is the only problem. I think it has something to do with the return type (void) that gets inputted as parameter (handler_t*) into Signal – FranXh Mar 17 '13 at 03:37