Question

I've a class created in a DLL (which uses /clr runtime, ManagedC++) and a constructor defined in that class. Code as follows:

//Following is defined in something.h//

namespace ABC
{
public ref Class XYZ
{
public: int a;
public: XYZ();
};

//In something.cpp, I've the below code to define the constructor of the class created//

#include something.h

namespace ABC
{
 XYZ::XYZ()
  {
     a = 100;
  }
}

Above project is built into a DLL

In another project, I try to use the Class XYZ as follows:

#include something.h
using namespace ABC;

//inside main, I've following code

{
ABC::XYZ^ prq = gcnew ABC:XYZ();
prq->a=200;

......
...
}

In this, I get the an error saying -

unresolved token (06000001) ABC.XYZ::.ctor

Could you please help what's the problem here?

Was it helpful?

Solution

The problem is that the linker can't find the definition of the constructor. It is located in another DLL. In a managed project, you solve that by adding a reference to the assembly. Right-click your project, Properties, Common Properties, Framework and References. Click the Add New Reference button. Use the Project tab if the project is located in the same solution. The Browse tab otherwise.

Also note that you now no longer need the .h file anymore. Declarations are imported from the metadata in the assembly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top