简体   繁体   中英

C-callable wrapper around C++ class

I am following a tutorial to create a wrapper around C++ code, so that it can be called from C#.
I get an error compiling the wrapper though. Header.h

class MyClass{
    public:
        MyClass(int x, int y);
        double GetSum();
    private:
        int x_;
        int y_;
};

Below is the source file (body.cpp)

#include  "Header.h"

MyClass::MyClass(int x, int y)
{
    x = 8;
    y = 8;
}

double MyClass::GetSum()
{
    int r = x_ + y_;
    return r;
}

The wrapper class/dll is as below

#include "C:\Users\tumelo\Documents\Visual Studio 2012\Projects\Emgu\MyClassCpp\MyClassCpp\Header.h"
#include "C:\Users\tumelo\Documents\Visual Studio 2012\Projects\Emgu\MyClassCpp\MyClassCpp\Body.cpp"

//for the function you want made avaible from the dll
    extern "C" __declspec(dllexport) double GetResults(int x, int y)
    {
        //create an instance of the class 
        MyClass myClass(int  x, int y);
        return myClass.GetSum(); 
    }

I get an in the wrapper class right at the return statement. The class method does not seem to be recognised for some reason. The error reads:

error C2228: left of '.GetSum' must have class/struct/union

What puzzles me is that this is copy and paste from the tutorial but mine does not work. What could I be missing?

You meant:

MyClass myClass(x, y);

instead of

MyClass myClass(int  x, int y);

What you typed is declaring a function named "myClass" that returns a "MyClass" instance and takes two integers. You meant to instantiate a variable named "myClass" of type "MyClass" by by passing it x and y.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM