Hi. I am wondering if a log file can be created in a header which is associated with a try-catch block. Below is my stripped down code for the log_header.h and log_test.cpp

Here is log_header.h

using namespace std;

class thrower{
    fstream myfile2;
    void errormessage();

void thrower::errormessage(){
    cout<<"Thrower works\n";
    ///////These are log commnands////////
    myfile2<<"Message 2\n"<<endl;

Here is log_test.cpp


using namespace std;

thrower t;
int main(){

    fstream myfile;

        int y=4;
        if (y<5){
            throw t;
    catch(thrower tt){
        myfile<<"Message \n"<<endl;
    return 0;

If I comment out all of the log_header.h lines that contain "myfile2", then the "myfile" object in the log_test.cpp gets updated and the error message from the errormessage method is printed. It would be more elegant if the log file creation was contained in the header. Please assist.

This article has been dead for over six months. Start a new discussion instead.