• 0

Stupid C++


Question

C++ is a real pain in the ass. Consider the following code, which is a real-world example (from one of my homeworks) :

globals.h :

#ifndef GLOBALS_H_INCLUDED
#define GLOBALS_H_INCLUDED

#include <pthread.h>
#include "sortie.h"
#include "source.h"

class globals {
public:
	static pthread_mutex_t mutexSource; // Sert a eviter que deux threads essayent d'acceder a la source en meme temps
	static pthread_cond_t condSourcePleine; // Sert a ce que la source puisse attendre lorsque la file est pleine.
	static pthread_cond_t condSourceVide; // Sert a ce que les ordinateurs puissent attendre lorsque la file est vide.
	static int nombreSorties; // Indique le nombre total de sorties disponibles. Inferieur a MAXSORTIES.
	static sortie **sorties; // Tableau des sorties
	static source* src; // Pointeur vers la source
}

#endif // GLOBALS_H_INCLUDED

globals.cpp :

#include "globals.h"

pthread_mutex_t globals::mutexSource;
pthread_cond_t globals::condSourcePleine;
pthread_cond_t globals::condSourceVide;
int globals::nombreSorties;
sortie **globals::sorties;
source* globals::src;

This code doesn't compile; g++ says on line 3 of globals.cpp : expected initializer before 'globals'. Wow, how helpful. I can google that error message for 2 hours and not get any kind of relevant information.

First, just to get those static public variables to both compile and link properly, I have to re-state their existence in a separate cpp file, and yeah, that was from googling, there's no way I ever could've guessed that.

But anyway, as it turned out after 15 minutes of staring blankly at that perfect-looking code, I discovered I was missing a ';' after the class declaration. What? It's 2009 and my compiler can't tell me I'm missing a ****ing semicolon.

OMG. :angry:

Sometimes I feel genuinely stupid after discovering a mistake such as forgetting a '&' when I'm passing an address or things of that nature, but here I just feel like C++ has utterly wasted 15 minutes of my time.

Thanks for allowing to vent, it feels good.

Edited by Dr_Asik
Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Compilers aren't really obvious sometimes and a compiler that shows exactly what's wrong for complex languages such as C/C++ aren't easy to write.

You have to keep in mind, however, that it complained about something before "globals" on the cpp file. That line was correct, so you can assume that something BEFORE that threw the compiler off. And since includes behave as if the included file were right there, then when you looked at the .h you'd see the missing ;

Edit: just thought I'd mention that, if you're doing CompSci, then you'll see sooner or later how difficult it is to write a proper compiler error message (how/when to save data to enable the compiler to know where and what the problem is)

Link to comment
Share on other sites

  • 0
Compilers aren't really obvious sometimes and a compiler that shows exactly what's wrong for complex languages such as C/C++ aren't easy to write.

You have to keep in mind, however, that it complained about something before "globals" on the cpp file. That line was correct, so you can assume that something BEFORE that threw the compiler off. And since includes behave as if the included file were right there, then when you looked at the .h you'd see the missing ;

Edit: just thought I'd mention that, if you're doing CompSci, then you'll see sooner or later how difficult it is to write a proper compiler error message (how/when to save data to enable the compiler to know where and what the problem is)

In my experience, when I'm working with Java or C#, the compiler error messages are generally straight to the point, especially for trivial errors such as a missing semicolon, whereas 50% of the time in C++ I have to try to understand why the compiler is saying some completely unrelated crap. I thought I had become good at that, but apparently I still need practice.
Link to comment
Share on other sites

  • 0
In my experience, when I'm working with Java or C#, the compiler error messages are generally straight to the point, especially for trivial errors such as a missing semicolon, whereas 50% of the time in C++ I have to try to understand why the compiler is saying some completely unrelated crap. I thought I had become good at that, but apparently I still need practice.

I too used to think that the error messages for C++ really should be improved especially after all these years, but I had a chance to work on a C++ compiler project a couple of years ago and believe me.... its not pretty when you sit down with a grammar like C++ (compared to Java/C#) and abide by all the rules. Now, when I get a compilation error, I first think what is the most stupidest and novice thing I could do ... and usually that gives me the answer in a jiffy (except for the errors from C++ templates of course).

Link to comment
Share on other sites

  • 0
Compilers aren't really obvious sometimes and a compiler that shows exactly what's wrong for complex languages such as C/C++ aren't easy to write.

You have to keep in mind, however, that it complained about something before "globals" on the cpp file. That line was correct, so you can assume that something BEFORE that threw the compiler off. And since includes behave as if the included file were right there, then when you looked at the .h you'd see the missing ;

Edit: just thought I'd mention that, if you're doing CompSci, then you'll see sooner or later how difficult it is to write a proper compiler error message (how/when to save data to enable the compiler to know where and what the problem is)

Not really. C/C++ compilers just suck :)

Link to comment
Share on other sites

  • 0
Not really. C/C++ compilers just suck :)
IMO, that's pedantic. A lot of very smart people have worked on C++ compilers for decades now, so for C++ compilers to be so cryptic, C++ itself must be a wild beast. My guess is that compilers reflect the inherent logic and consistency of the language; if C++ compilers are cryptic, it must be that C++ is a cryptic language.
Link to comment
Share on other sites

  • 0

Haha I've had that error before and it's taken a while to find it. I think that seems to be the one place that can throw weird errors if you're missing a semi-colon. I know you use Visual Studio but after you recommended Code::Blocks I gave it a shot and using the code completion would stop you ever getting that error.

In Code::Blocks type class then CTRL + J and it inserts a basic template. I'm sure VS must have the similar tool.

Link to comment
Share on other sites

  • 0
Haha I've had that error before and it's taken a while to find it. I think that seems to be the one place that can throw weird errors if you're missing a semi-colon. I know you use Visual Studio but after you recommended Code::Blocks I gave it a shot and using the code completion would stop you ever getting that error.

In Code::Blocks type class then CTRL + J and it inserts a basic template. I'm sure VS must have the similar tool.

Thanks for the tip, CTRL+J works great. I just also discovered there's a class wizard in the Plugins menu that's pretty nifty. That's one more mistake I won't be making again.

As for VS, it has quite a few Code Wizards, although I've never used any, as of yet.

Link to comment
Share on other sites

  • 0

Yeah I'd never used any sort of code completion either but it is a bit of a time saver and anything that helps reduce compile errors is great! The next time I'm programming with classes I'll give the class wizard a go.

Link to comment
Share on other sites

This topic is now closed to further replies.