A virtual statement canceling the 'error LNK2001: unresolved external symbol' error?
I tried to generate a “applepie.dll”, which used some functions defined in a header file called ‘Apple.h’. But the compiler showed error in linking process as below:
applepie.obj : error LNK2001: unresolved external symbol "public: double __thiscall Apple::Function2(void)" (?Function2@Apple@@QAENPBD@Z)
.\ applepie.dll : fatal error LNK1120: 1 unresolved externals
The ‘Apple’ application is used to generate another ‘Apple.dll’
Code:
//Apple.h////////////////////////////////////////////////
Class Apple{
public:
double Function1(const char* arg);
double Function2(void);
}
//Apple.cpp////////////////////
double Apple::Function1(const char* arg){
//Do something here;
}
double Apple::Function2(void){
//Do something here;
}
Please let me describe how the problem happened in more detail. In ApplePie.cpp, I try to use the functions defined in Apple.cpp like:
Apple* m_Apple;
m_Apple=something //some method was used to get the pointer to class Apple.
double weight;
weight= m_Apple-> Function2();
The compilation process does not show error, but the link error will show up. I searched online, and included ‘Apple.lib’ in the ‘applepie’ application using Project->settings->link, following the instructions to similar ‘error LNK2001: unresolved external symbol’ problems that many people have, but this did not help.
However, I found that if I put ‘virtual’ before the definition of function 2, like:
virtual double Function2(void);
then the compiler can link successfully, no error shows up.
I am totally confused about what’s happening here, why a ‘virtual’ statement could cancel the linking error?