Beddu Beddu - 1 year ago 178
C++ Question

Porting from C++ to C++/CLI

I do not really believe that this question was never asked before, but i really tried to search without success, if you got a link to an already answered similar question, please share it.

I am porting a C++/Win32 program to C++/CLI, and, of course, I am trying to make the fewer number of modifications possible to the code in order to speed-up the re-testing phase.

I am having some trouble due to global functions having objects as parameters, here a short example:

Class header file

namespace MyNamespace {
public ref class MyClass {
void test();

Class cpp file

using MyNamespace;

void myFunction(MyClass ^obj);

void MyClass::test() {

And here comes the problem: if I leave out myFunction from MyNamspace, it cannot use MyClass as a parameter's type. If I include myFunction in MyNamespace, every cpp file will compile correctly, but i will get a linker error "LNK2028 unresolved token".

An idea is to define a new class and include myFunction as a public static method, but to do this will be a long job, because myFunction, in the real project, is not alone... Any other idea?

Answer Source

If MyClass is in MyNamespace and myFunction is not, you could use MyClass with full name qualification:

void myFunction(MyNamespace::MyClass ^obj);

Of course your myFunction should be implemented somewhere.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download