Cannot instantiate abstract class

Do not post support requests, bug reports or feature requests. Discuss CEF here. Non-CEF related discussion goes in General Discussion!

Cannot instantiate abstract class

Postby gee » Mon Oct 25, 2010 10:39 pm

I'm running into a strange problem; possibly I'm not understanding a simple C++ concept:

Code: Select all
class CefHandlerDefaultImpl : public CefThreadSafeBase<CefHandler> {
public:
    virtual RetVal HandleXXX1(...) { return RV_CONTINUE; }
    ...
    virtual RetVal HandleXXXn(...) { return RV_CONTINUE; }
};
class MyCefClient : public CefHandlerDefaultImpl {
    ...
    ...
    ...
};
int APIENTRY wWinMain(...) {
    ...
    MyCefClient aClient = new MyCefClient(); // ERROR C2259: cannot instantiate abstract class
    ...
}

What is it that I'm missing here? Please and thank you.
gee
Newbie
 
Posts: 4
Joined: Mon Oct 25, 2010 10:24 pm

Re: Cannot instantiate abstract class

Postby magreenblatt » Tue Oct 26, 2010 8:44 am

ERROR C2259: cannot instantiate abstract class

CefHandler is an abstract class because it contains pure virtual methods. As a result, classes extending CefHandler must provide implementations for all of these pure virtual methods or those classes will also be considered abstract. You're most likely seeing this error because either your child class is missing method implementations or your method declarations don't match.

Regards,
Marshall
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Cannot instantiate abstract class

Postby gee » Tue Oct 26, 2010 3:16 pm

magreenblatt wrote:You're most likely seeing this error because either your child class is missing method implementations or your method declarations don't match.

I know for sure that I'm implementing all of the HandleXXX methods. My opinion is that enums such as RetVal etc. should not be typedef'd inside the CefHandler class definition to avoid these kinda issues (correct me if I'm wrong please).
gee
Newbie
 
Posts: 4
Joined: Mon Oct 25, 2010 10:24 pm

Re: Cannot instantiate abstract class

Postby magreenblatt » Tue Oct 26, 2010 3:38 pm

My opinion is that enums such as RetVal etc. should not be typedef'd inside the CefHandler class definition to avoid these kinda issues (correct me if I'm wrong please).

You're wrong :-). See http://msdn.microsoft.com/en-us/library ... 80%29.aspx
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Cannot instantiate abstract class

Postby gee » Tue Oct 26, 2010 5:40 pm

This is such a weird design in the sense that, if I want to create a default implementation class for CefHandler (returning RV_CONTINUE from all of the methods) and derive from this class overridding only the events I want to handle (returning RV_HANDLED), I simply cannot do it w/out the ugly kludges.

May be you or someone else can suggest a solution to me.
gee
Newbie
 
Posts: 4
Joined: Mon Oct 25, 2010 10:24 pm

Re: Cannot instantiate abstract class

Postby magreenblatt » Tue Oct 26, 2010 5:58 pm

You might want to look at existing code that does this -- see tests/unittests/test_handler.h and the classes that extend it (for instance, RequestSendRecvTestHandler in request_unittest.cc).
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Cannot instantiate abstract class

Postby gee » Tue Oct 26, 2010 6:35 pm

This is awkward but I'm gonna admit it; when I originally created CefHandlerDefaultImpl, I copied and pasted all methods from CefHandler to my class and implemented them. In this all copy-paste and find-replace procedure, I forgot to remove a <code>= 0</code> after one of the method signatures. I feel so stupid and apologize for getting upset without reason.
gee
Newbie
 
Posts: 4
Joined: Mon Oct 25, 2010 10:24 pm

Re: Cannot instantiate abstract class

Postby magreenblatt » Tue Oct 26, 2010 7:01 pm

No worries, live and learn :-)
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm


Return to CEF Discussion

Who is online

Users browsing this forum: No registered users and 22 guests