The C++ Error challenge

Started by
55 comments, last by Radikalizm 12 years, 5 months ago
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. :)
Advertisement
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.

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.
Indeed. That error is a lot harder to identify when the code isn't pasted into the forum.

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.
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'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.
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.
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]
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
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...

This topic is closed to new replies.

Advertisement