c++ error messages

Example C++ code with error messages

I’ve been working a bit with GNU C/C++ (version 11.3) on Linux Mint 21. I’ve been trying to use Visual Studio Code for coding, thinking that it, along with Microsoft’s extension, would help me write more effectively by pointing out problems/errors and providing suggested solutions. Sometimes that works, sometimes it doesn’t, meaning that I get the error but no idea how to solve it, no clues whatsoever. Rather, if I take the problematic code and let GNU C++ try to compile it, I get very verbose errors that include helpful suggestions on how to correct the problem.

This post uses a very simple program that requires C++ in order to compile and execute. Here’s the correct listing.

#include <iostream>#include <iterator>int main(int argc, char *argv[]) {std::copy(argv, argv + argc, std::ostream_iterator<char *>(std::cout, "\n"));}

It’s just six lines of code (five if you ignore the whitespace). The key line is line 5, a one-liner that prints out all the arguments passed along to the application when it’s called, one argument/line. I didn’t create this, but I learned it decades ago and have kept it in my “back pocket” along with many other bits of proven and useful code.

The problem occurred in another application when I failed to include a library that required an iterator definition. I eventually fixed it, but only on the command line using GNU’s C++. I then whipped up this example to show what I’m complaining about. If you look at the top screen capture you’ll see that VS Code has put red squiggles underneath the problem code. I’ve deliberately used an ostream_iterator that calls out for the inclusion of the iterator library. Unfortunately, as you’ll note above, clicking on the bad code in VS Code does nothing more than state the obvious. No clue how you might fix this. And before you say anything, I’ve worked with other IDEs in the past that actually did offer suggestions, and even offered to fix it for you. For example, Android Studio and the IDE it was derived from, JetBrains’ IntelliJ IDE. Granted those tools are all about Java and Kotlin, but still, they’re a lot more helpful and productive.

In the end it was GNU’s C++ that provided all I needed to fix the contrived problem.

cli.cpp: In function ‘int main(int, char**)’:cli.cpp:5:39: error: ‘ostream_iterator’ is not a member of ‘std’5 | std::copy(argv, argv + argc, std::ostream_iterator<char *>(std::cout, "\n"));  |   ^~~~~~~~~~~~~~~~cli.cpp:2:1: note: ‘std::ostream_iterator’ is defined in header ‘<iterator>’; did you forget to ‘#include <iterator>’?1 | #include <iostream>  +++ |+#include <iterator>2 | //#include <iterator>

This help included not only what library definition to include, but where (yes, I commented out the original to break it). Line five in the error listing shows you where ostream_iterator is defined, and line 7 helpfully shows you where to place the include in your code. This kind of compiler help reminds me of the help that Rust’s compiler also produces when it comes across a problem.

My experience with C++ goes back to 1986 when it was first called C with Classes and you had to run cfront on your C++ code to produce C code that was then compiled and linked. All this on a Micro VAX running Ultrix, DEC’s version of BSD Unix (which was nothing more than bog-standard BSD with all the licensing text replaced with DEC). I know it’s so much fun denigrating C++, but it’s evolving to answer the critiques and the needs of its users, both in the language as well as the tooling.