Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8611303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:20:38+00:00 2026-06-12T04:20:38+00:00

I have been following this guide on how to call a member function of

  • 0

I have been following this guide on how to call a member function of a C++ object from C. As I’ve understood it, the C code should interpret the class as a struct of the same name, and whenever it wants to call a function through an object of this class it should use an intermediate callback function. The header looks like this:

// CInterface.h
#ifdef __cplusplus
...

class CInterface 
{
public:
    ...

    void OnMessage(U8* bytes); // I want to call this function from C.

private:
    ...
};
#else
typedef
    struct CInterface
      CInterface;
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if defined(__STDC__) || defined(__cplusplus)
  //extern void c_function(CInterface*);   /* ANSI C prototypes (shouldn't be needed) */
  extern CInterface* cpp_callback_function(CInterface* self, unsigned char * bytes);
#else
  //extern void c_function();        /* K&R style (shouldn't be needed) */
  extern CInterface* cpp_callback_function(unsigned char * bytes);
#endif

#ifdef __cplusplus
}
#endif

The C code that fails right now looks like this:
// main.c
#include “CInterface.h”

int main(int argc, char* argv[])
{
    void* ptr;
    int *i = ptr; // Code that only compiles with a C compiler
    CInterface cinterface; // This should declare a struct
}

The error is: error C2079: ‘cinterface’ uses undefined struct ‘CInterface’.

It sounds like the header is being read as c++ code as the struct is not defined, but main.c is being compiled by C according to Visual Studio (I also double checked this by adding some C-specific code). However, if I add parentheses like this:

CInterface cinterface();

the code compiles which makes no sense to me as it now is an object which shouldn’t work in C.

The callback function is implemented in a third file, CInterface.cpp, which acts as the “intermediate”.

So the question is how I solve this error message, or if I got the entire approach wrong. It’s the first time I mix C/C++ code and I’m relatively new to both languages.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:20:39+00:00Added an answer on June 12, 2026 at 4:20 am

    In your example CInterface is only defined for C++. If you take a closer look at the example you linked you’ll notice that this is also the case for the Fred class.

    From C you can only pass around pointers to CInterface and you have to rely on C++ functions defined with C linkage to actually manipulate CInterface instances.

    Otherwise you could define a struct as a means to pass around data between C and C++. Just ensure its definition is declared as extern "C" when used from C++:

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    struct CandCPlusPlus {
    // ...
    };
    
    #ifdef __cplusplus
    }
    #endif
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to NHibernate... I have been following this NHibernate Tutorial from Gabriel Schenker
I have been following the answer of this question: How to update existing object
I have been working on the following problem from this book . A certain
I have been following the validation for Entry boxes from here . The code
I have been trying to get a basic reorderlist working following this guide ->
I have been following this guide: http://www.icodeblog.com/2010/07/08/asset-libraries-and-blocks-in-ios-4/ However, this example calls the assetGroupEnumerator with
I have been following this great guide on setting up bluetooth between 2 iPhones.
I have been following this guide , which goes over adding a file to
I can't believe how unbelievably complicated this has been... I have the following XML...
I have been following the affableBean tutorial from the NetBeans site located here .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.