#ifndef TEST_H
#define TEST_H
#include "Expections.h"
class test
{
public:
test();
void set(int x);
private:
int size;
};
#endif //
The Error I get is
test.cpp|14|undefined reference to `Expections::Expections(std::string const|
could someone explain why it does not work or how I could go about fixing it.
Thank You
You've forgotten to provide a definition for Exception::Exception(const string &mes). If you don't use a constructor, then there's no need to provide a definition for it. However, in your case, you've requested the use of the constructor, and therefore, you must provide a definition for the constructor.
Can you use exceptions without constantly having to use try & catch?
Only catch the exception if you can handle the error in a meaningful way. Otherwise you let the exception be thrown to a higher level. Hopefully someone will eventually catch the exception that knows what to do.
The code doesn't work because std::exception doesn't have constructor taking a string argument. Either override the what() function or throw/inherit from some other exception class that has a string constructor. http://www.cplusplus.com/reference/std/stdexcept/
So I changed the Expections(const string & message) to just print the message but is there a way to have the program terminate after displaying the message. Right now if I do that it will terminate but my compiler becomes unresponsive.
is there a way to have the program terminate after displaying the message.
To make the program terminate after the exception has been thrown you can catch the exception in main() and let the program terminate by returning from main as normal. If you don't catch the exception the program will also end but that might display an error message.
khal wrote:
Right now if I do that it will terminate but my compiler becomes unresponsive.
Yes the IDE becomes unresponsive. Its strange that the code in my text does not use the try & catch function it just throws a string. Anyway thanks for your help