CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    3

    "extern "C" void (*)(int)" cannot be converted to type "void (*)(int)"

    Hello,

    could you help me with the following error ?

    1 // ConsoleClose.cpp
    2
    3 #include "StdAfx.h"
    4
    5 #include "ConsoleClose.h"
    6
    7 #include <signal.h>
    8
    9 static int g_BreakCounter = 0;
    10 static const int kBreakAbortThreshold = 2;
    11
    12 namespace NConsoleClose {
    13
    14
    15 #ifdef __cplusplus /* the __cplusplus macro */
    16 extern "C" void HandlerRoutine(int); /* is automatically defined */
    17 #else /* by the C++/MVS compiler */
    18 void HandlerRoutine(int);
    19 #endif
    20
    21 static void HandlerRoutine(int)
    22 {
    23 g_BreakCounter++;
    24 if (g_BreakCounter < kBreakAbortThreshold)
    25 return ;
    26 exit(EXIT_FAILURE);
    27 }
    28
    29 bool TestBreakSignal()
    30 {
    31 return (g_BreakCounter > 0);
    32 }
    33
    34 void CheckCtrlBreak()
    35 {
    36 if (TestBreakSignal())
    37 throw CCtrlBreakException();
    38 }
    39
    40
    41 CCtrlHandlerSetter::CCtrlHandlerSetter()
    42
    43 {
    44 memo_sig_int = signal(SIGINT,HandlerRoutine); // CTRL-C
    45 if (memo_sig_int == SIG_ERR)
    46 throw "SetConsoleCtrlHandler fails (SIGINT)";
    47 memo_sig_term = signal(SIGTERM,HandlerRoutine); // for kill -15 (before "kill -9")
    48 if (memo_sig_term == SIG_ERR)
    49 throw "SetConsoleCtrlHandler fails (SIGTERM)";
    Compile error:

    "../../UI/Console/ConsoleClose.cpp", line 21.13: CCN5400 (S) "NConsoleClose::HandlerRoutine(int)" has a conflicting declaration.
    "../../UI/Console/ConsoleClose.cpp", line 16.18: CCN5424 (I) "HandlerRoutine" is declared on line 16 of "../../UI/Console/ConsoleClose.cpp".


    If I remove "static" in line 21, I get :

    "../../UI/Console/ConsoleClose.cpp", line 44.25: CCN5216 (S) An expression of type "extern "C" void (*)(int)" cannot be converted to type "void (*)(int)".
    "../../UI/Console/ConsoleClose.cpp", line 45.8: CCN5207 (S) No common type found for operands with type "void (*)(int)" and "extern "C" void (*)(int)".
    "../../UI/Console/ConsoleClose.cpp", line 47.26: CCN5216 (S) An expression of type "extern "C" void (*)(int)" cannot be converted to type "void (*)(int)".
    Regards,
    Nenad

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: "extern "C" void (*)(int)" cannot be converted to type "void (*)(int)"

    C language doesn't support namespaces, so extern "C" and namespaces are incompatible. Function defined inside of a namespace cannot be declared as extern "C".

  3. #3
    Join Date
    Feb 2013
    Posts
    3

    Re: "extern "C" void (*)(int)" cannot be converted to type "void (*)(int)"

    Quote Originally Posted by Alex F View Post
    C language doesn't support namespaces, so extern "C" and namespaces are incompatible. Function defined inside of a namespace cannot be declared as extern "C".
    Hello,

    thank you for the reply - this code compiles successfully on cygwin unix emulator. Looks like it's up to compiler (IBM XL C/C++ in my case). What can be done in code to resolve this ?

    Regards,
    Nenad

  4. #4
    Join Date
    Jul 2002
    Posts
    2,543

    Re: "extern "C" void (*)(int)" cannot be converted to type "void (*)(int)"

    Just move HandlerRoutine outside of the namespace.

  5. #5
    Join Date
    Feb 2013
    Posts
    3

    Re: "extern "C" void (*)(int)" cannot be converted to type "void (*)(int)"

    1 // ConsoleClose.cpp
    2
    3 #include "StdAfx.h"
    4
    5 #include "ConsoleClose.h"
    6
    7 #include <signal.h>
    8
    9 static int g_BreakCounter = 0;
    10 static const int kBreakAbortThreshold = 2;
    11 static void HandlerRoutine(int)
    12 {
    13 g_BreakCounter++;
    14 if (g_BreakCounter < kBreakAbortThreshold)
    15 return ;
    16 exit(EXIT_FAILURE);
    17 }
    18 bool TestBreakSignal()
    19 {
    20 return (g_BreakCounter > 0);
    21 }
    22
    23
    24 namespace NConsoleClose {
    25
    26
    27 bool TestBreakSignal()
    28 {
    29 return (g_BreakCounter > 0);
    30 }
    31
    32 void CheckCtrlBreak()
    33 {
    34 if (TestBreakSignal())
    35 throw CCtrlBreakException();
    36 }
    37
    38
    39 CCtrlHandlerSetter::CCtrlHandlerSetter()
    40
    41 {
    42 memo_sig_int = signal(SIGINT,HandlerRoutine); // CTRL-C
    43 if (memo_sig_int == SIG_ERR)
    44 throw "SetConsoleCtrlHandler fails (SIGINT)";

    "../../UI/Console/ConsoleClose.cpp", line 42.33: CCN5256 (S) A parameter of type "extern "C" void (*)(int)" cannot be initialized with an expression of type "void (int)".
    "../../UI/Console/ConsoleClose.cpp", line 42.33: CCN6205 (I) The error occurred while converting to parameter 2 of "signal(int, extern "C" void (*)(int))".
    "../../UI/Console/ConsoleClose.cpp", line 43.8: CCN5207 (S) No common type found for operands with type "void (*)(int)" and "extern "C" void (*)(int)".
    "../../UI/Console/ConsoleClose.cpp", line 45.35: CCN5256 (S) A parameter of type "extern "C" void (*)(int)" cannot be initialized with an expression of type "void (int)".
    "../../UI/Console/ConsoleClose.cpp", line 45.35: CCN6205 (I) The error occurred while converting to parameter 2 of "signal(int, extern "C" void (*)(int))".
    "../../UI/Console/ConsoleClose.cpp", line 46.8: CCN5207 (S) No common type found for operands with type "void (*)(int)" and "extern "C" void (*)(int)".
    "../../UI/Console/ConsoleClose.cpp", line 52.18: CCN5256 (S) A parameter of type "extern "C" void (*)(int)" cannot be initialized with an expression of type "void (*)(int)".
    "../../UI/Console/ConsoleClose.cpp", line 52.18: CCN6205 (I) The error occurred while converting to parameter 2 of "signal(int, extern "C" void (*)(int))".
    "../../UI/Console/ConsoleClose.cpp", line 53.19: CCN5256 (S) A parameter of type "extern "C" void (*)(int)" cannot be initialized with an expression of type "void (*)(int)".
    "../../UI/Console/ConsoleClose.cpp", line 53.19: CCN6205 (I) The error occurred while converting to parameter 2 of "signal(int, extern "C" void (*)(int))".
    CCN0793(I) Compilation failed for file ../../UI/Console/ConsoleClose.cpp. Object file not created.
    FSUM8226 make: Error code 12
    FSUM8226 make: Error code 255

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured