Feature request for the compiler team


I want to preface this by saying that the MS compiler team have worked wonders in improving developer productivity over the years, and can’t be thanked enough for turning the C++ language into an enterprise quality platform that has lasted more than a decade.

That said, “C2259: ‘CFoo’ : cannot instantiate abstract class” is probably one of the least useful of the compiler errors.  This error occurs on a line where I am trying to instantiate a CFoo object, and the compiler has  determined that CFoo is an abstract class.  The problem is that most of the time CFoo isn’t supposed to be an abstract class.  I’m trying to instantiate it, so chances are good that, semantically, I don’t intend CFoo to be an abstract class.  The compiler, however, has determined that there is some pure virtual function that isn’t overridden, and therefore the class is abstract and cannot be instantiated.

The reason this is a problem is that, in most cases, the bug is not that I’m trying to instantiate the class.  The bug is that CFoo hasn’t overridden a pure virtual function that needs to be overridden so that CFoo is not abstract.  The compiler does a good job of flagging down that there’s an error, but it’s unhelpful because in most cases, it’s flagging down the wrong error.

So what?  The compiler can’t be expected to discern programmer intent.  That’s why programmers make the big bucks, right?  Lots of error codes are cryptic so why single this one out?  Because it’s fixable.  It’s not an unknowable problem.  It’s just an oversight is all.  The answer isn’t even technically difficult from a compiler’s POV.  The compiler has already determined that my class is abstract.  It should be easy from there to tell me why the compiler thinks my class is abstract.

C2259: ‘CFoo’ : cannot instantiate abstract class.  CFooBase::Bar(int, void*, const char*) is not overridden.

Comments (5)

  1. tzagotta says:

    That’s a good change request. Obviously the compiler would have the relevant information to issue that message. I think that there used to be some concern or design limitation about multi-line error messages, i.e., violating the idea that each error condition would just produce one error line.

    Most other C++ compilers (e.g., Borland) tell you the reason that the class is abstract. It is a helpful error message.

  2. Maurits says:

    Hmm… consider

    class IA : { void Foo() = 0; }

    class IB : IA { void Bar(int, void*, const char*) = 0; };

    class CC : IA { /* oops, forgot to override Foo() */ };

    class CD: IB { /* oops, forgot to override Foo() AND Bar(…) */ };

    I would propose the following errors:

    IA a; // should error "cannot instantiate abstract class IA: IA::Foo() is pure virtual"

    IB b; // should error "cannot instantiate abstract class IB: IB::Bar(int, void*, const char*) is pure virtual"

    CC c; // should error "cannot instantiate abstract class CC: pure virtual function Foo() from IA"

    CD d; // should error "cannot instantiate abstract class CD: two pure virtual functions including Foo() from IA"

    but the CC and CD errors are tricky.

  3. Stewart Tootill says:

    What version of the microsoft compiler are you using? It certainly already shows this on my version (Visual studio 2005) and I’m sure it did on at least the previous version.The error message comes out as follows:

    c:projectssomefile.cpp(271) : error C2259: ‘sub_outputter’ : cannot instantiate abstract class

           due to following members:

           ‘void base_class::on_data(const BYTE *,size_t)’ : is abstract

           c:projectsbase_class.h(16) : see declaration of ‘base_class::on_data’

    I don’t know if this is optional (I’ve certainly never deliberately turned it on)

  4. RyanBemrose says:

    Interesting.  The one I’m using (from the Windows build tree) is actually marginally newer than what ships with VS2k5 Enterprise Gold  (14.00.50727.198 vs 14.00.50727.42), yet doesn’t appear to do this.  It might be something to take up with the build guys.

  5. JamesCurran says:

    Ryan,

    Are you looking at the "Error List" window or the "Output" window.

    The Former will only contain the lines in the format:

    c:projectssomefile.cpp(###) : error C#### …….

    with the sequent clarifying lines removed.

    The latter will have the full error messages.