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


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.


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.


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

 virtual vid b();

and it works OK.
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.

VapiSoftAuthor Commented:
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.

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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.