How to have exception handler in separate .H & .cpp

Hi,

I'm having an issue when trying to transfer my exception handler from my main cpp file into a separate header and cpp file. Unfortunately nothing on google is matching what I need, there's plenty on a separate header but I need a header and source file.

So I have this in my main file atm, but a simple copy and paste into the two separate files isn't working so I'm wanting to know if it needs to be changed for the other cpp definition file?

My code which I have been copying into my exception.h
1
2
3
4
class Open_error : public logic_error{
    public:
    Open_error (string reason):logic_error(reason){};


This code has remained in main cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
   try
   {    
        infile.open(inName.c_str());
        if (infile.fail())
            throw Open_error("Cannot open file.");
   }
    catch (Open_error& reason)
    {
        cout << reason.what();
        return 1;
    }


   outfile.open(outName.c_str());


I've currently got this in my exception.cpp, I know it's wrong but I really have no idea what to do to fix it.

1
2
3
4
5
6
7
#include "exception.h"
#include <stdexcept>

Argc_error::Open_error(string reason)
    :logic_error{}
    {
    }





This is my error message which I don't get when all the above is just in my main function:

>g++ -Wall test.cpp exception.cpp
exception.cpp: In constructor 'Open_error::Open_error(std::string)':
exception.cpp:5:6: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
exception.cpp:5:18: error: no matching function for call to 'std::logic_error::logic_error(<brace-enclosed initializer list>)'
exception.cpp:5:18: note: candidates are:
In file included from exception.h:7:0,
                 from exception.cpp:1:
c:\mingw-4.7.1\bin\../lib/gcc/mingw32/4.7.1/include/c++/stdexcept:63:5: note: std::logic_error::logic_error(const string&)
c:\mingw-4.7.1\bin\../lib/gcc/mingw32/4.7.1/include/c++/stdexcept:63:5: note:   candidate expects 1 argument, 0 provided
c:\mingw-4.7.1\bin\../lib/gcc/mingw32/4.7.1/include/c++/stdexcept:56:9: note: std::logic_error::logic_error(const std::logic_error&)
c:\mingw-4.7.1\bin\../lib/gcc/mingw32/4.7.1/include/c++/stdexcept:56:9: note:   candidate expects 1 argument, 0 provided



I'm hoping I can get away with providing this small level of detail.

In my main cpp I have included the exception.h header, and using namespace std. I'm not interested in a discussion of whether that's correct or not, it needs to be included.

Thanks so much for your time.

Last edited on
> I'm hoping I can get away with providing this small level of detail.
post a minimal example that does reproduce your issue.
I don't want to have to fill blanks.

¿why does the class name change to `Argc_error'?


Going only on the error message, `logic_error' constructor wants an string.
Topic archived. No new replies allowed.