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 rpn
to 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.