0

Every time I add a non-concrete virtual method to an abstract class, the compiler gives unuseful error invalid new-expression of abstract class type '...' on all the classes that derives from that abstract class, and then I need to do git diff to search for the new method I added earlier, or look for the note in GCC's errors. (there can be days difference between adding the method, and compiling)

Can I specify in C++ that a class must be concrete (and if it's not tell the cause/missing-method)?

LyingOnTheSky
  • 2,844
  • 1
  • 14
  • 33
  • 1
    Can you clarify what "look for the note in GCC's errors" means, and in particular, why is doing that not what you want? A compiler error report consists of all messages related to that error, not just the single line saying "error." – Angew is no longer proud of SO Feb 21 '18 at 09:40
  • 3
    [Modern compilers emit a rather explicit message](https://godbolt.org/g/rZsYMC)... – user7860670 Feb 21 '18 at 09:41
  • @Angew because lots of files are compiled at once (it's a makefile), and GCC writes all kind of stuff about them. Searching for the error, and reading the lines below consumes lots of time, the task I said in the question happens a lot. – LyingOnTheSky Feb 21 '18 at 09:42
  • @VTT See my comment. – LyingOnTheSky Feb 21 '18 at 09:42
  • 2
    *"there can be days difference between adding the method, and compiling"* - Either I completely misunderstand what you mean, or you have one heck of an unusual development routine. – StoryTeller - Unslander Monica Feb 21 '18 at 09:43
  • Question title asks "Make the compiler tell which methods are not concrete", compilers do this by printing base class and method that are not implemented making this class abstract. – user7860670 Feb 21 '18 at 09:43
  • Can't reproduce your problem. All compilers which I have tested gives a error message like "because the following methods are abstract"... So please use a up to date compiler! – Klaus Feb 21 '18 at 09:43
  • The problem is that there can be 20 missing member functions, so they simply wouldn't fit into a single error message. The thing is, the compiler *is* telling you the missing function(s). You just have to read its entire output. – Angew is no longer proud of SO Feb 21 '18 at 09:43
  • @Angew if I specify that the class is concrete, then regardless if I do new, then the compiler should make an error for each missing member that prevents the class to be concrete, it sounds reasonable for me. – LyingOnTheSky Feb 21 '18 at 09:53
  • @VTT I said in my question that my compiler does that, it's just not comfortable searching these notes in a long output log. – LyingOnTheSky Feb 21 '18 at 09:54
  • @LyingOnTheSky How will changing the format from "note" to "error" make it more comfortable? – Angew is no longer proud of SO Feb 21 '18 at 09:54
  • @Angew I grep "error: ". (greping a note will result in giving me 50% of the output) – LyingOnTheSky Feb 21 '18 at 09:57
  • Then grep "following virtual functions are pure" or something not so generic... Also this implies that you are getting huge amount of errors so you should compile more often fixing problems immediately and preventing them from piling up. – user7860670 Feb 21 '18 at 09:58
  • We either don't understand you or there is something wrong with the way you are doing this. This really shouldn't be an issue. – bolov Feb 21 '18 at 09:58
  • 2
    or grep with `-B -A -C -n` https://stackoverflow.com/questions/9081/grep-a-file-but-show-several-surrounding-lines – bolov Feb 21 '18 at 09:59
  • @VTT it's warnings, not errors. (the people I cooperate with, don't care about the existence of them) – LyingOnTheSky Feb 21 '18 at 10:01
  • @bolov didn't want this, but I will live with this for the moment. – LyingOnTheSky Feb 21 '18 at 10:03
  • You could be interested in my [question about telling GCC diagnostics apart](https://stackoverflow.com/q/21905742/1782465), there could be some solution for you in there. – Angew is no longer proud of SO Feb 21 '18 at 10:06
  • then create a small script that parses the error messages/logs and prints you just the information you want: classes/methods in question. You can format this output to your heart's delight. – bolov Feb 21 '18 at 10:06

1 Answers1

2

Answering the question at hand:

Can I specify in C++ that a class must be concrete (and if it's not tell the cause/missing-method)?

C++ has no explicit abstract or concrete classes. These concepts are implicit in the language. Abstract is implied by the presence (either in the class itself or inherited) of one or more pure virtual member functions, and concrete by the absence of such functions.

This is exactly how the C++ standard defines an abstract class (§13.4/2)

[class.abstract]

An abstract class is a class that can be used only as a base class of some other class; no objects of an abstract class can be created except as subobjects of a class derived from it. A class is abstract if it has at least one pure virtual function.

There's no keyword such as Java's abstract, for example, in C++.

Compiler error messages are the standard place where you can catch errors such as the attempt to instantiate a class that still has pure virtual member functions.

Community
  • 1
  • 1
JBL
  • 12,588
  • 4
  • 53
  • 84