Normally these sort of errors means that there is a mismatch between function declaration and their bodies. I have checked that all the classes has exact number of function implementation as defined in their .h respective classes. All projects in same solution and on same platform (x86). I think this is not duplicate question as I tried to read all of the questions on forum related to this problem.
I have an MFC Project which work fine on its own. I added a class named MathFuncs.cpp with header file so that I can use that in Wrapper Class. I want to use Wrapper class in my C# project.
I have tried many different ways to develop the wrapper class project, MFC DLL, Normal Class Library and Win32 MFC Dynamically Linked Libraries. I have changed different settings too like adding dependency project, including external directories but no use. I keep on getting LNK 2028, 2019 and 1120 Error.
If I don't give reference of other class object in MathFuncs.cpp then every thing works fine but as soon as I start creating other classes (in same project) objects and using their functions the CLR (DLL) project gets upset with it. I am not sure where I am wrong. Please note that both projects are in same solution of VS2010.
Following is the Code of MathFuncs.h:
#pragma once
#include <stdexcept>
#include "ESCppClientDlg.h"
using namespace std;
class MyMathFuncs
{
public:
MyMathFuncs();
double Add(double a, double b);
double Subtract(double a, double b);
double Multiply(double a, double b);
double Divide(double a, double b);
//char *getResultValue();
//private:
// CESCppClientDlg dlg;
};
and Mathfuns.h
#pragma once
#include "MathFuncs.h"
double MyMathFuncs::Add(double a, double b)
{ return a+b; }
double MyMathFuncs::Subtract(double a, double b)
{ return a-b; }
double MyMathFuncs::Multiply(double a, double b)
{ return a*b; }
double MyMathFuncs::Divide(double a, double b)
{
if ( b == 0 )
throw invalid_argument("b cannot be zero!");
return a/b;
}
//char * MyMathFuncs::getResultValue()
// {
//
//
// return dlg.GetResultValue();
// }
MyMathFuncs::MyMathFuncs()
{
CWnd *pParent = (CWnd *)0;
CESCppClientDlg dlg = new CESCppClientDlg();
}
Following is the code of Class Library
// ClassLibrary1.h
#pragma once
#include "Stdafx.h";
#include "C:\Programming\C#\LiecaClientLibrary\ESCppClient Solution 13082015 - V2\MathFuncs.h"
#include "C:\Programming\C#\LiecaClientLibrary\ESCppClient Solution 13082015 - V2\MathFuncs.cpp"
using namespace System;
namespace CppWrapper {
public ref class MyMathFuncsWrapper
{
public:
// constructor
MyMathFuncsWrapper()
{
initVal = 20.0;
myCppClass = new MyMathFuncs(); //initiate C++ class's instance
}
double AddWrapper ( double a, double b)
{ return myCppClass->Add(a,b); }
double SubtractWrapper (double a, double b)
{ return myCppClass->Subtract(a,b); }
double MultiplyWrapper (double a, double b)
{ return myCppClass->Multiply(a,b); }
double DivideWrapper (double a, double b)
{ return myCppClass->Divide(a,b); }
// public variable
double initVal;
private:
MyMathFuncs *myCppClass; // an instance of class in C++
};
}
If I comment
CWnd *pParent = (CWnd *)0;
CESCppClientDlg dlg = new CESCppClientDlg();
from MathFuncs.cpp then everything works fine but I really need to use CESCppClientDlg's object to access the data. That was the whole point to make wrapper class. Following is the Error I am getting:
Error 1 error LNK2028: unresolved token (0A000613) "public: __thiscall CESCppClientDlg::CESCppClientDlg(class CWnd *)" (??0CESCppClientDlg@@$$FQAE@PAVCWnd@@@Z) referenced in function "public: __thiscall MyMathFuncs::MyMathFuncs(void)" (??0MyMathFuncs@@$$FQAE@XZ) C:\Programming\C#\LiecaClientLibrary\ESCppClient Solution 13082015 - V2\ClassLibrary1\ClassLibrary1.obj ClassLibrary1 Error 2 error LNK2019: unresolved external symbol "public: __thiscall CESCppClientDlg::CESCppClientDlg(class CWnd *)" (??0CESCppClientDlg@@$$FQAE@PAVCWnd@@@Z) referenced in function "public: __thiscall MyMathFuncs::MyMathFuncs(void)" (??0MyMathFuncs@@$$FQAE@XZ) C:\Programming\C#\LiecaClientLibrary\ESCppClient Solution 13082015 - V2\ClassLibrary1\ClassLibrary1.obj ClassLibrary1 Error 3 error LNK1120: 2 unresolved externals C:\Programming\C#\LiecaClientLibrary\ESCppClient Solution 13082015 - V2\Debug\ClassLibrary1.dll ClassLibrary1
Many Thanks in Advance