Udostępnij za pośrednictwem


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

  • Anonymous
    December 08, 2006
    The comment has been removed
  • Anonymous
    December 08, 2006
    The comment has been removed
  • Anonymous
    December 10, 2006
    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)
  • Anonymous
    December 11, 2006
    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.
  • Anonymous
    December 13, 2006
    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.