Here is an exception defined in <stdexcept>:
class length_error : public logic_error
{
public:
explicit length_error(const string& __arg);
};
Here is my exception:
#include <string>
#include <stdexcept>
using namespace std;
class rpn_expression_error : public logic_error
{
public:
explicit rpn_expression_error(const string& __arg);
};
Why do I get this error when <stdexcept> does not?
Undefined symbols:
rpn_expression_error::rpn_expression_error(/*string*/ const&), referenced from:
...
ld: symbol(s) not found
At @sbi‘s request, here is a minimal example of my code at the moment:
#include <string>
#include <iostream>
#include <stdexcept>
using namespace std;
class RPN_Calculator {
public:
class rpn_expression_error : public logic_error {
public:
explicit rpn_expression_error(const string& arg) : logic_error(arg) {}
};
void Execute() {
throw rpn_expression_error("Hello");
}
};
int main() {
RPN_Calculator calc;
try {
calc.Execute();
} catch (exception e) {
cout << e.what() << endl;
}
}
I saved this as rpn.cpp and ran make rpnto produce the error.
The code now builds completely, however, the real program still gives me the original error.
Note/Solution: Although the code above runs just fine, the same exception class in the real code still produces the linker error. To simplify, I just promoted rpn_expression_error to its own global-scope class, and that seems to have fixed the problem.
There is a problem with the way you are catching your exceptions. Specifically, consider this code:
On that marked line,
dgets what is called "sliced". That is, in order to satisfy being aBase, everything that isn’t part ofBasegets sliced off! So the above code will output "Base!".If we want the intended output, we need to make the parameter not a value:
Our above code would then display "Derived!", because it’s no longer being sliced. (One could also use a pointer.)
So, take a look at your catch clause:
Here, any exceptions you’ve thrown will be sliced into the base
std::exceptionclass, losing any derived information! This is why it’s much more common (and possibly "correct") to catch-by-reference:You’ll now find
e.what()returns the non-sliced error message, as intended.Old
Here’s how the entire thing should look (don’t use
using namespacein a header!):Older
Because those exception classes are declared inside the standard namespace, but yours is not.
stringis inside the namespacestdso they don’t need to qualify it, but you do:Keep in mind I’ve changed your parameter name. Names that contain a double-underscore are reserved, and you shouldn’t use them.