Saturday 15 March 2014

C++ operator<< overloading ofstream using templates -



C++ operator<< overloading ofstream using templates -

this question has reply here:

operator overloading on class templates 5 answers

i trying overload operator<< ostream writing log file mechanism. within cexportfunctions project, able log << "xxx". however, when tried perform same in project (ccallmethods), unable write file. compilation okay. no errors. entered processmessage() did not written file. can 1 please help?

project - cexportfunctions.h: #ifdef dlldir_ex #define dlldir __declspec(dllexport) // export dll info #else #define dlldir __declspec(dllimport) // import dll info #endif ... class dlldir cexportfunctions { public: ... ofstream stream; }; project - cexportfunctions.cpp: #include "cexportfunctions.h" ... //! write log file template<typename t> cexportfunctions& operator<<(cexportfunctions& stream, t val) { ... stream.stream.open("d:/logger/logs.txt", ios::out | ios::app); stream.stream << << val << std::endl; stream.stream.close(); homecoming stream; } //! save scenario dialog void cexportfunctions::savescenario() { cexportfunctions log; log << "entered savescenario()"; ... } project b - ccallmethods.cpp: #include "cexportfunctions.h" void ccallmethods::processmessage() { ... cexportfunctions log; log.stream << "entered processmessage()"; }

you're calling different functions. in save scenario:

//! save scenario dialog void cexportfunctions::savescenario() { cexportfunctions log; log << "entered savescenario()"; ... }

you're calling your

template<typename t> cexportfunctions& operator<<(cexportfunctions& stream, t val)

but sec one:

void ccallmethods::processmessage() { ... cexportfunctions log; log.stream << "entered processmessage()"; }

you're calling operator<<(std::ofstream&, const char*)... doesn't involve opening file. think meant:

log << "entered processmessage()";

c++ templates operator-overloading dllexport

No comments:

Post a Comment