954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Code compilation error

Hello People,
I just came across this code as I was browsing through some C++ problem sets. This code gives compile time error but I am unable to understand the reason. I am C guy and my knowledge about C++ is not that great. So pardon if there is an obvious solution to this problem. Any help would be appreciated

class Base
{
public:
      
     Base();    
     virtual ~Base();
};

class derived: public Base
{
public:                      
       virtual ~derived();
};

  
int main()  
{
    Base *pb = new derived;    
    getch();
}
abhimanipal
Master Poster
742 posts since Dec 2009
Reputation Points: 114
Solved Threads: 104
 
This code gives compile time error but I am unable to understand the reason.


Which error?
Only thing that I see missing, is a header file for getch(); .

[EDIT]
Oh, and it leaks memory ;)

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 
Which error? Only thing that I see missing, is a header file for getch(); .


Whoops, it is also lacking the implementation of constructor/destructors.

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

Hello People, I just came across this code as I was browsing through some C++ problem sets. This code gives compile time error but I am unable to understand the reason. I am C guy and my knowledge about C++ is not that great. So pardon if there is an obvious solution to this problem. Any help would be appreciated

class Base
{
public:
      
     Base();    
     virtual ~Base();
};

class derived: public Base
{
public:                      
       virtual ~derived();
};

  
int main()  
{
    Base *pb = new derived;    
    getch();
}

You defined the virtual generator function,I think it is illegal in C++ grammar
The sixth line and twelfth line

sheff_cc
Newbie Poster
20 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 
You defined the virtual generator function,I think it is illegal in C++ grammar The sixth line and twelfth line


No, it is certainly not so i.e. avirtual destructor is perfectly legal.

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

Wow,I think i made a mistake.
You got it

sheff_cc
Newbie Poster
20 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You