wrapper class CLR

hello, I need a wrapper class to connect c++ to C# but it gives me 5 errors, first one says: 'st' : 'struct'type redefinition.
compiler sees the link but this error gets annoying.

in header:

using namespace System;

namespace CppWrapper {

public ref class CppWrapperClass
{
public:


CppWrapperClass();
//~CppWrapperClass();
int getSum();
int sum;

private:

st * p = new st;
};
}

in source file:

CppWrapper::CppWrapperClass ::CppWrapperClass()
{

}

int CppWrapper::CppWrapperClass::getSum()
{
int sum;
sum = p->get();
return sum;

}

in c++:

#include<iostream>
using namespace std;
struct st {
int ma[3] = { 1,2,3 };
int get() { return ma[2]; }
};

int main() {

return 0;
}
thank you!

Last edited on
First of all, please edit your post and put code tags around the individual files that you posted. Highlight the lines and click the Fomat button that looks like "<>". This will make is much easier to read and comment on your code.

Second, it seems like you are leaving some stuff out. For instance, your header file is not being included in either of the source files that you show.

Third, you don't tell us which file was being compiled when you got the error message. I can't quite make out the order of includes and to guess where the error actually is. I think you are doing something funky like including main.cpp (or whatever you call it) in CppWrapperClass.cpp.

However, defining struct st in the source file containing main() and then using it in your header file is a bad idea. Either define it in the header file or define it in another header file that the header includes. You don't want a situation where the header file is included but the struct is not defined.
thank you for helpful answer, I want to know why it did not run when main() containing file was called?
now it works but I really want to know way it did not work. way it only executes only in header?
In my second point, I mentioned that you must be leaving stuff out of what you posted. What you posted would not give a redefinition error. There will be lots of other errors, but not a redefinition.

Why it did not work is buried in the code you did not post. What #includes were in each file? How did you build? What file was being built when you got the 5 errors? What were the exact error messages (including file names and line numbers)? Without this information, it is pretty difficult to determine what exactly went wrong.


Topic archived. No new replies allowed.