Definition of class:
#pragma once #include <string> #include <utility> namespace impress_errors { #define BUFSIZE 512 class Error { public: Error(int number); Error(std::string message); Error(const char *message); bool IsSystemError(); std::string GetErrorDescription(); private: std::pair <int, std::string> error; char stringerror[BUFSIZE]; // Buffer for string describing error bool syserror; }; } // namespace impres_errors I have some piece of code in file posix_lib.cpp:
int pos_close(int fd) { int ret; if ((ret = close(fd)) < 0) { char err_msg[4096]; int err_code = errno; throw impress_errors::Error::Error(err_code); //Call constructor explicitly } return ret; } And in another file fs_creation.cpp:
int FSCreation::GenerateFS() { int result; try { result = ImprDoMakeTree(); //This call pos_close inside. } catch (impress_errors::Error error) { logger.LogERROR(error.GetErrorDescription()); return ID_FSCREATE_MAKE_TREE_ERROR; } catch (...) { logger.LogERROR("Unexpected error in IMPRESSIONS MODULE"); return ID_FSCREATE_MAKE_TREE_ERROR; } if(result == EXIT_FAILURE) { return ID_FSCREATE_MAKE_TREE_ERROR; } return ID_SUCCESS; } On my version of compiler this one is compiled and work correct:
g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5 (Ubuntu Maverick - 10.04)
On another version of compiler:
g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2 (Ubuntu Narwhal - 11.04) it causes
error:
posix_lib.cpp: In function ‘int pos_close(int)’:
posix_lib.cpp:363:46: error: cannot call constructor ‘impress_errors::Error::Error’ directly
posix_lib.cpp:363:46: error: for a function-style cast, remove the redundant ‘::Error’
Questions:
1. Why this work on one version of g++, and failed on another?
2. What happens when I explicitly call constructor for creating object? Is it correct?
And why this is working?