Expections question

main.cpp
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "test.h"
using namespace std;

int main()
{

    return 0;
}


Expections.h
1
2
3
4
5
6
7
8
9
#include<string>
using namespace std;

class Expections
{
    public:
        Expections(const string & mes);

};


test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "test.h"
#include <iostream>

using namespace std;

test::test()
{
    size = 0;
}

void test::set(int x){

    if(x > 5){
        throw Expections("This is wack");
    }

}


test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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
closed account (zb0S216C)
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.

Wazzak
Can you use exceptions without constantly having to use try & catch?
This is the code inside my text and it doesnt work.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<string>
#include <exception>
using namespace std;

class Expections
{
    public:
        Expections(const string & message) 
       {  cout << message << endl;

}

};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef TEST_H
#define TEST_H
#include "Expections.h"

class test
{
    public:
        test();
        void set(int x) throw (Expections);

    private:
        int size;
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "test.h"
#include <iostream>

using namespace std;

test::test()
{
    size = 0;
}

void test::set(int x){

    if(x > 5){
        throw Expections("This is wack");
    }
}
Last edited on
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/
Last edited on
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.
khal wrote:
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.

You mean the IDE 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
Topic archived. No new replies allowed.