1

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?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Tumelo
  • 301
  • 3
  • 18
  • Did you add the `.dll` as a reference to your project? – Greg Feb 12 '15 at 14:22
  • @Tumelo: Please at least *try* to reduce your problem. In the course of that, you will either isolate and solve it yourself (and get better and faster at it very soon), or at least have the chance for a good question. I removed most of the irrelevant cruft. BTW: You should think about using relative include-paths instead of absolute ones. – Deduplicator Feb 12 '15 at 14:28
  • @Deduplicator thanks, I will use relative paths. – Tumelo Feb 12 '15 at 23:27

1 Answers1

5

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.

Moby Disk
  • 3,761
  • 1
  • 19
  • 38