I'm trying to create a exception handling class for a large-ish project I'm working on which inherits from std::exception. I found this tutorial at cplusplus.com and followed its basic instructions and used its derived class as a template. Here is my Exception class as it stands right now:
#include <exception>
#include "../types.h"
namespace nuclear
{
class Exception : public std::exception
{
public:
Exception(string_t msg)
{
error = msg;
}
~Exception() throw();
virtual const char* what() const throw()
{
return error.c_str();
}
private:
string_t error;
};
} // !namespace nuclear
And here is the short test program to try to use it (note the commented-out part):
#include <iostream>
#include "../types.h"
#include "exception.h"
using namespace std;
int main()
{
nuclear::Exception e("testing...");
/*try
{
throw e;
}
catch (exception& e)
{
cout << e.what() << endl;
}
*/
return 0;
}
No matter what I do, I get this error:
/tmp/ccGK1bXn.o: In function `main':
exception_test.cpp:(.text+0x1d5): undefined reference to `nuclear::Exception::~Exception()'
/tmp/ccGK1bXn.o: In function `nuclear::Exception::Exception(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
exception_test.cpp:(.text._ZN7nuclear9ExceptionC1ESs[nuclear::Exception::Exception(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)]+0x1d): undefined reference to `vtable for nuclear::Exception'
collect2: ld returned 1 exit status
What I really want is to be able to define an Exception object as it is thrown, like this:
void dostuff()
{
// do stuff here... oops, something goes wrong!
throw nuclear::Exception e("Error: you messed up!");
}
Is that possible? Thank you for your help!