Hello, please help, point out errors in the implementation of tassirovka. I can not understand the principle of operation of #ifdef , #else and #define .
I implement class Trace.h with a template method for writing. In it I implement my macros. Can you please tell me if I'm on the way? The file TRACE.txt is in the same place as the executable file.
Bottom line: does not write anything to the file.
I think that the error in the definition of macros.
Code:
(Trace.h)
#include <iostream> #include <fstream> class Trace { public: Trace() { std::ofstream TraceFileWrite("TRACE.txt"); } template <class T> void writingData(T object) { TraceFileWrite << T; } ~Trace() { // TraceFileWrite.close(); } }; #ifdef Enabled_Trace #define Enabled_Trace(object) writingData(object) #else #define Enabled_Trace(object) #endif (Source.cpp)
#include "Trace.h" #include <iostream> #include <string> int main() { int a=7; double b=93.09432; std::string str("Hello"); Enabled_Trace(a) Enabled_Trace(b) Enabled_Trace(str) std::cout<<"Hello Trace!"<<std::endl; system("pause"); return 0; }
TraceFileWriteclassstd::ofstream, which is destroyed when you exit the constructor. - user239133