dimanche 25 janvier 2015

differences in constructor syntax when creating exceptions (c++)

So background, I have a strong background in the Java programming language. I am a total noob when it comes to the C++ language. Basically I know that there are two different syntaxes for defining constructors in C++, I just do not understand the difference.


When writing the an exception constructor as follows (in a manor similar to a method without a return type), the compiler outputs a no matching function for call to 'std::runtime_error::runtime_error()' error:



#include <stdexcept>
using namespace std;

class DivideByZeroException : public runtime_error {
public :
DivideByZeroException() {
runtime_error("attempted to divide by zero.");
}
};


But, when the constructor is modified to the following, the code compiles error-free:



#include <stdexcept>
using namespace std;

class DivideByZeroException : public runtime_error {
public :
DivideByZeroException() :
runtime_error("attempted to divide by zero."){};
};


Could someone please explain what is going on here?


Aucun commentaire:

Enregistrer un commentaire