• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

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

Hi,

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.




 

0
VapiSoft
Asked:
VapiSoft
  • 3
  • 2
1 Solution
 
sarabandeCommented:
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.

Sara

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

A
{
 virtual vid b();
};

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

Sara
0
 
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.



0
 
sarabandeCommented:
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.

Sara
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now