[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


How to call a member of a class in another DLL.

Posted on 2011-10-06
Medium Priority
Last Modified: 2012-05-12

I have a class with a member (function)

class A
 void b();

A *a=new A;

now I pass the a as a parameter to a DLL function.

So now in the DLL there is a defenition:
A *a;

Now I try to call be from the DLL:  a->b();

The problem is that I get unresolved error when I compile the DLL.


Question by:VapiSoft
  • 3
  • 2
LVL 35

Expert Comment

ID: 36925505
that doesn't work that way.

you would need to have an own dll where the class A was implemented. then you could export the class by using the __declspec(dllexport) specifier. if done so far you could add the import library which was built with the dll to both your application project and the dll project and both can use the class A. you can use a common header file for class A which uses __declspec(dllimport) as specifier when it was used for import.



Author Comment

ID: 36925831
I did solve the problem by adding "virtual" infront of the void b();

 virtual vid b();

and it works OK.
LVL 35

Expert Comment

ID: 36925895
the 'unresolved external' error was a linker error which could not be solved using the virtual keyword. the virtual was a compiler directive that tells the function b might get virtually overloaded by a class derived from A. the compiler then would add code to support that functionality. the linker nevertheless would need the implemented and compiled object code of function A::b() and it would complain if missing.

I assume you added the a.cpp to the dll project what is a workaround.


Author Comment

ID: 36926189
It is very interesting.
I don't have the b() function in the DLL.
before I added the virtual I received the unresolved, but now I don't.

But in the caller DLL I recieved the unresolved error even after I added the virtual, and it was resolved only after I defined the A:b(){} function.

LVL 35

Accepted Solution

sarabande earned 2000 total points
ID: 36926907
that is what i told you.

i didn't know that you reported a linker error of your application project (and if you look at your initial q. you will see that you only told of a dll error)..

you don't need the A::b() in the app project if you don't call the b function from there. here the virtual keyword could cause a changed behavior though the error was not really justified.

as told a workaround to use the A::b() in the dll could be to add the a.cpp to the project. but you shouldn't use a copy of the source but should use a common folder where the a.cpp resides. a 3rd aletrnative is to create a static library out of class A and add the lib to both projects.


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question