0

I am trying to write a wrapper class which wraps around another class containing c based header files and functions.

Here is the original problem to which I am trying to find a workaround.

This is the class which calls the c functions and should be encapsulated:

#include "c_header.h"

class A
{
public:
          void foo () { 
          function();  //calling a function of the c based library
        }; 
};

Here is how I created the wrapper class (.lib) to encapsulate the class A :

#include "A.h"
class wrapper  
{
public:
    void  test()
        {
          wa-> foo()
        };
private:
    A* wa;
};

And here is the test project in which I was hoping to call the wrapper class library without the need to know about the class A (c_header.h and its functions).

#include "wrapper.h"

void main(){
    wrapper *w = new wrapper;
    w->test();
}

The test main does not compile and issues linker problems complaining about the functions inside class A (here function()).

The codes are in windows7 and visual studio2015.

Could anyone help me with really encapsulating/wrapping another class without the need for the header files?

Thanks in advance.

  • 3
    Not the problem but `void main()` is not correct. `main()` must always have an `int` return type. – NathanOliver May 13 '19 at 14:51
  • C and C++ are different languages. Everyday they diverge more and more. My 2 cents are: Write a `.h` header to be included in `.c` files, and another `.hpp` header with the same name to be included in `.cpp` files; even if 90% of the code is the same. You will need to maintain twice the code, but you will avoid most of the madness of handling two different languages in one file. – alx - recommends codidact May 13 '19 at 14:59

1 Answers1

2

You need to tell the compiler/linker it's a C header:

extern "C" {
#include "c_header.h"
}
Paul Evans
  • 27,315
  • 3
  • 37
  • 54