I used numbers to indicate my analysis please follow in that order. I am using the code snippets compiled using g++.
using namespace std;
class Base
{
public:
virtual void fun()
{
cout<<"Base";
p=new Base();
/** 3. for p: virtual pointer to virtual table of Base
* since it contains only a pointer size is 4bytes
**/
Derived d;
/**4. compiled successfully, but linker issued an error
* 5. Creates a pointer to a virtual table of d
* 6. function is bound on some name in virtual table (lets say id02)
* 7. Entry of virtual table could be as below
* (ex: id02(binding name), fun(unresolved reference which has to be resolved during linking phase)
**/
/* Ignore below line as analysis not needed now */
p->fun();
return 0;
}
bash-3.00$ g++ vptr.cc
Undefined first referenced
symbol in file
vtable for Derived /var/tmp//ccmN6VG1.o
ld: fatal: Symbol referencing errors. No output written to a.out
1. What type of error we got?
A) The above line indicates it is a linker error. (Meaning lexical, syntax, semantic etc are through)
2. What is that symbol?
A) Vtable for derived (some convention used to indicate such unresolved references)
collect2: ld returned 1 exit status
8. During the linking phase, it searches for the unresolved reference (lets say in ST) and ends in an un successful attempt as there was no definition and issues an error.
ld went mad hence exited with status 1 resulting in such scary red lines
No comments:
Post a Comment