Skip to main content
GameDev.net gamedev.net
🔒 Locked

The C++ Error challenge

Started by Serapth Nov 9, 2011 at 4:10 PM 55 replies 9.8k views
Original Post
Serapth
Serapth
I recently made a post ( DON'T READ if you are going to play along, the answer is in that post ) about one of those little C++'isms that I think make the language poorly suited for new developers. I don't really want to get into that old argument in this thread. I stated within the post that veteran C++ coders will have been bitten by this error in the past so would probably instantly identify the error but a new developer would basically be screwed without Google. Now I am wondering if that is true, so I want to make a bit of a game of it.


First off, DONT POST THE ANSWER in this thread!

I am going to post the offending code and the error generated. If you want to play along, post in here how long approximately it took you to figure ( or post to say you don't know ) the error's cause out and send me a quick PM with what you believe the answer is. Optionally in your post state the amount of C++ experience you have. I will then respond to your post in this thread with a yes or no, depending on if the answer you PMed me was correct. EDIT: Or use the spoiler tag I didn't realize we had... I need to hang out in off topic threads more...

Please do not Google the answer, or if you do, mention that in your post!

I really am curious if it is one of those things people would get immediately or not and how long it would take. Keep in mind, it's not about finding flaws in the code, it's specifically about the compiler error in question.


The code:

/////////////////////////////////// SoundFileCache.h //////////////////////////////////////////

#pragma once

#include "SFML/Audio.hpp"

class SoundFileCache
{
public:
SoundFileCache(void);
~SoundFileCache(void);

const sf::Sound& GetSound(std::string) const;
const sf::Music& GetSong(std::string);

private:
static std::map<std::string, sf::Sound> _sounds;
static std::map<std::string, sf::Music> _music;
};


class SoundNotFoundExeception : public std::runtime_error
{
public:
SoundNotFoundExeception(std::string const& msg):
std::runtime_error(msg)
{}
}



/////////////////////////////////// SoundFileCache.cpp //////////////////////////////////////////

#include "StdAfx.h"
#include "SoundFileCache.h"


SoundFileCache::SoundFileCache(void) {}

SoundFileCache::~SoundFileCache(void) {}


const sf::Sound& SoundFileCache::GetSound(std::string soundName) const
{
std::map<std::string,sf::Sound>::iterator itr = _sounds.find(soundName);
if(itr == _sounds.end())
{
sf::SoundBuffer soundBuffer;
if(!soundBuffer.LoadFromFile(soundName))
{
throw new SoundNotFoundExeception(
soundName + " was not found in call to SoundFileCache::GetSound");
}

sf::Sound sound;
sound.SetBuffer(soundBuffer);
_sounds.insert(std::pair<std::string,sf::Sound>(soundName,soundBuffer));
}
else
{
return itr->second;
}
}

const sf::Music& SoundFileCache::GetSong(std::string soundName)
{
//stub
}





The error:

sfmlsoundprovider.cpp(6): error C2533: 'SFMLSoundProvider::{ctor}' : constructors not allowed a return type



Have fun.
alvaro
alvaro
I might have been programming C++ for too long, but it took me about 5 seconds to get the answer. I've probably gotten a similar error before.
Antheus
Antheus

I might have been programming C++ for too long, but it took me about 5 seconds to get the answer. I've probably gotten a similar error before.


/sigh

Same...Didn't even read through code, just scanned for the offender.

llvm actually fixes this and similar issues in a completely helpful manner and reports the real problem.
Serapth
Serapth

Indeed. That error is a lot harder to identify when the code isn't pasted into the forum.


This is true. It is also a hell of a lot harder to identify if you haven't been bitten by it, too.


Coincidentally, nobody is PMing the answer... there is always the outside chance you are in fact wrong. Additionally, I am assuming everyone that got it instantly have a fair bit of exposure to C++?


Also, there is no shame in not knowing the answer if you are intimidated by the answers thus far.
Zlodo
Zlodo
I'm not going to say that C++ is perfect, but that example says more about the compiler that you use than about the language.

There is no reason that this particular mistake couldn't be diagnosed more accurately by the compiler. I can't remember what gcc does in this situation from the top of my head. I don't recall it outputting an error message that actually makes sense but at least the error that is reported is located in the general vicinity of the actual mistake iirc.

Can't wait for clang to implement lambdas so I can switch over to it.
Serapth
Serapth

I'm not going to say that C++ is perfect, but that example says more about the compiler that you use than about the language.

There is no reason that this particular mistake couldn't be diagnosed more accurately by the compiler. I can't remember what gcc does in this situation from the top of my head. I don't recall it outputting an error message that actually makes sense but at least the error that is reported is located in the general vicinity of the actual mistake iirc.

Can't wait for clang to implement lambdas so I can switch over to it.


I would agree with you, with the exception that every single compiler has loads of examples like this. So, some compilers might be better in some cases, while in other areas they are much worse. Much of this stems from the (complexity of the) language design itself.
Zlodo
Zlodo
I would agree with you, with the exception that every single compiler has loads of examples like this.

I won't know for certain until I have been able to get some experience with clang, but with the amount of efforts they are dedicating to the quality of error messages I would be surprised if it was as prone to output confusing or obscure error messages as either visual c++ or gcc.
Cornstalks
Cornstalks
Took about 30 seconds. I'm not going to PM you, but instead I'll use a spoiler. I've been bitten by it, though I usually find the error in less than a few minutes. Usually it just takes a few seconds to find it. I can't remember what my compiler says... maybe it's got a more helpful message that helps me find it faster.

[spoiler]There's no ';' at the end of the class definition for [font="Courier New"]SoundNotFoundExeception[/font][/spoiler]
m3mb3rsh1p
m3mb3rsh1p
It took me a while (5-10 minutes). I don't have much experience outside of academia and passing hobby-coding so the error didn't jump at me.

I've found this error more quickly before.

I feel bad. Ah well...
Serapth
Serapth

It took me a while (5-10 minutes). I don't have much experience outside of academia and passing hobby-coding so the error didn't jump at me.

I've found this error more quickly before.

I feel bad. Ah well...


Yes.


And @Cornstalks, crap, didn't realize there was a spoiler tag... yeah, use it.
popsoftheyear
popsoftheyear

It took me a while (5-10 minutes). I don't have much experience outside of academia and passing hobby-coding so the error didn't jump at me.

I've found this error more quickly before.

I feel bad. Ah well...

Don't feel bad... I've run into this plenty of times - it's my job! - and completely missed it here (I ended up cheating to find the problem) blink.gif
TomKQT
TomKQT
Took me only a while, this kind of error is nasty from the beginning because it apparently doesn't give any sense at all. But after some time, you learn what to expect and look for

[spoiler]
Missing ; after class definition.
[/spoiler]
Telastyn
Telastyn

Coincidentally, nobody is PMing the answer... there is always the outside chance you are in fact wrong. Additionally, I am assuming everyone that got it instantly have a fair bit of exposure to C++?


I decided what I thought the problem was then followed the 'do not follow me!' link.

Haven't used it in about 5 years, but wrote almost exclusively in it for... 8? years before that? So yeah, some exposure.
TomKQT
TomKQT
I don't think anyone who wanted to solve the problem himself would read all the replies first, so I'll post this even though it MAYBE could give a very TINY hint (unintentional):

It's "funny" when errors similar to this one turn your perfect code into something with hundrets of errors. That can absolutely horrify a beginner. And the solution is just [spoiler]one character[/spoiler] away.
brx
brx
Took me about one minute, but it would have been easier, if
a) you would have named the file according to the error message
and

[spoiler]
b) you would have put the .h in a separate code tag for clarity
a ";" at the end of a header is the first thing i look for when someone in a forum talks about "unexplainable" error messages....
[/spoiler]
Serapth
Serapth

Took me about one minute, but it would have been easier, if
a) you would have named the file according to the error message
and

[spoiler]
b) you would have put the .h in a separate code tag for clarity
a ";" at the end of a header is the first thing i look for when someone in a forum talks about "unexplainable" error messages....
[/spoiler]


You know what, that was actually the error generated. When I initially encountered the problem it generated the error:
error C2533: 'SoundFileCache::{ctor}' : constructors not allowed a return type
So when I recompiled, to copy/paste the error here, it gave:
error C2533: 'SFMLSoundProvider::{ctor}' : constructors not allowed a return type

Instead.

[spoiler]
So truth of the matter is, had I included the code for SFMLSoundProvider instead of SoundFileCache, it would have been even MORE of a red herring. It proves just how insidious this error message is, in that it could pop up in any included class, not just the implementation file of the class with the header missing a semicolon!
[/spoiler]
Washu
Washu

[quote name='Telastyn' timestamp='1320855759' post='4882177']
Indeed. That error is a lot harder to identify when the code isn't pasted into the forum.


This is true. It is also a hell of a lot harder to identify if you haven't been bitten by it, too.


Coincidentally, nobody is PMing the answer... there is always the outside chance you are in fact wrong. Additionally, I am assuming everyone that got it instantly have a fair bit of exposure to C++?


Also, there is no shame in not knowing the answer if you are intimidated by the answers thus far.
[/quote]

I didn't even bother reading the error, I just scanned the code for the most commonly encountered errors I see n00bs making. The one in the code above is so frequent, it's almost at the top of my list.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
Serapth
Serapth

[quote name='Serapth' timestamp='1320856099' post='4882180']
[quote name='Telastyn' timestamp='1320855759' post='4882177']
Indeed. That error is a lot harder to identify when the code isn't pasted into the forum.


This is true. It is also a hell of a lot harder to identify if you haven't been bitten by it, too.


Coincidentally, nobody is PMing the answer... there is always the outside chance you are in fact wrong. Additionally, I am assuming everyone that got it instantly have a fair bit of exposure to C++?


Also, there is no shame in not knowing the answer if you are intimidated by the answers thus far.
[/quote]

I didn't even bother reading the error, I just scanned the code for the most commonly encountered errors I see n00bs making. The one in the code above is so frequent, it's almost at the top of my list.
[/quote]

I don't even think I would call it a noob error though. I've been using C++ for well over a decade and I still do it on occasion. Only difference is, I recognize the signs almost immediately.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.