Overloading << operator?
-
I've tried to overload the << oprator, so that I can output to a file and to a console at the same time but I have no success. I cannot find a solution in Internet and I get the error message: ambigous overload of operator <<... Any ideas what to do? I want to be able to use this operator to output something like: logfile << "data"; but still to be able to do this: cout << "out"; Thank you!
-
I've tried to overload the << oprator, so that I can output to a file and to a console at the same time but I have no success. I cannot find a solution in Internet and I get the error message: ambigous overload of operator <<... Any ideas what to do? I want to be able to use this operator to output something like: logfile << "data"; but still to be able to do this: cout << "out"; Thank you!
i'm not sure i understand. you want - in the same
<<
operation - that your datas are written both in a file and in a console ? there's no need to overload such an operator for this. you write a function say writeOutput(data_to_write) in which you perform acout << data
and afile << data
where file is an ifstream opened with the file to write... if not, please explain more clearly...
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20] -
i'm not sure i understand. you want - in the same
<<
operation - that your datas are written both in a file and in a console ? there's no need to overload such an operator for this. you write a function say writeOutput(data_to_write) in which you perform acout << data
and afile << data
where file is an ifstream opened with the file to write... if not, please explain more clearly...
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20] -
Yes you are right but I want to be able to use variable number of arguments but I don't want to use va_list, etc... C functions.
-
Yes you are right but I want to be able to use variable number of arguments but I don't want to use va_list, etc... C functions.
I don't understand how varargs fits into overloading << ? Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
I've tried to overload the << oprator, so that I can output to a file and to a console at the same time but I have no success. I cannot find a solution in Internet and I get the error message: ambigous overload of operator <<... Any ideas what to do? I want to be able to use this operator to output something like: logfile << "data"; but still to be able to do this: cout << "out"; Thank you!
-
Cristoff wrote:
but I don't want to use va_list, etc... C functions
however, it is the way to do it...
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20] -
I have done it that way but according to samples I've found in Internet, it has to be possible to do it with overloading << but I cannot get it to compile.
Cristoff wrote:
I've found in Internet
can i know this place ?
Cristoff wrote:
but I cannot get it to compile.
any chance to have a look at your "not-compiling" code (i don't want it all, i need only a piece of code to understand how you try to achieve the point we're talking about) ...?
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20] -
Cristoff wrote:
I've found in Internet
can i know this place ?
Cristoff wrote:
but I cannot get it to compile.
any chance to have a look at your "not-compiling" code (i don't want it all, i need only a piece of code to understand how you try to achieve the point we're talking about) ...?
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20] -
I've tried to overload the << oprator, so that I can output to a file and to a console at the same time but I have no success. I cannot find a solution in Internet and I get the error message: ambigous overload of operator <<... Any ideas what to do? I want to be able to use this operator to output something like: logfile << "data"; but still to be able to do this: cout << "out"; Thank you!
#include "stdafx.h" #include #include #include #include #include #include #include #include class Employer { friend ostream &operator <<(ostream &Obj, Employer &EmpObj) ; private : public: Employer() { //strcpy(m_name,"Amol Ravatale"); } ~Employer() { } char m_name[20]; }; ostream &operator <<(ostream &Obj, Employer &EmpObj) { Obj<>(istream &obj, Employer &EmpObj) { char str[30]; obj>>str; strcpy(EmpObj.m_name ,str); return obj; } int main(int argc, char**argv) { Employer emp; ofstream man("amol.txt") ; //creates a text file in application path strcpy(emp.m_name,"amol"); man<>emp; //cout<
-
#include "stdafx.h" #include #include #include #include #include #include #include #include class Employer { friend ostream &operator <<(ostream &Obj, Employer &EmpObj) ; private : public: Employer() { //strcpy(m_name,"Amol Ravatale"); } ~Employer() { } char m_name[20]; }; ostream &operator <<(ostream &Obj, Employer &EmpObj) { Obj<>(istream &obj, Employer &EmpObj) { char str[30]; obj>>str; strcpy(EmpObj.m_name ,str); return obj; } int main(int argc, char**argv) { Employer emp; ofstream man("amol.txt") ; //creates a text file in application path strcpy(emp.m_name,"amol"); man<>emp; //cout<
sorry , include these files #include "stdafx.h" #include #include #include #include #include #include #include #include
-
I've tried to overload the << oprator, so that I can output to a file and to a console at the same time but I have no success. I cannot find a solution in Internet and I get the error message: ambigous overload of operator <<... Any ideas what to do? I want to be able to use this operator to output something like: logfile << "data"; but still to be able to do this: cout << "out"; Thank you!
Here is basically what I'm trying to do:
#include #include // A singleton data logging class class Logger { public: // Since this class is a singleton, here is a static method to get a // pointer to it. static Logger &GetInstance() { static Logger logger; return logger; }; void Initialize() { if (!Log.is_open()) Log.open("logfile.txt"); }; void Shutdown() { if (Log.is_open()) { Log.close(); } }; protected: Logger() { }; std::ofstream Log; public: template friend std::ostream & operator << ( std::ostream & os, T data ); }; template std::ostream & operator << ( std::ostream & os, T data ) { os << data; std::cout << data; return os; }; int main() { std::cout << "Hello world!" << std::endl; Logger::GetInstance(); Logger::GetInstance().Initialize(); Logger::GetInstance()<< "ciao" << std::endl; std::cout << "Good nigght world!" << std::endl; return 0; }
...but unfortunately I don't know what I'm doing. -
Here is basically what I'm trying to do:
#include #include // A singleton data logging class class Logger { public: // Since this class is a singleton, here is a static method to get a // pointer to it. static Logger &GetInstance() { static Logger logger; return logger; }; void Initialize() { if (!Log.is_open()) Log.open("logfile.txt"); }; void Shutdown() { if (Log.is_open()) { Log.close(); } }; protected: Logger() { }; std::ofstream Log; public: template friend std::ostream & operator << ( std::ostream & os, T data ); }; template std::ostream & operator << ( std::ostream & os, T data ) { os << data; std::cout << data; return os; }; int main() { std::cout << "Hello world!" << std::endl; Logger::GetInstance(); Logger::GetInstance().Initialize(); Logger::GetInstance()<< "ciao" << std::endl; std::cout << "Good nigght world!" << std::endl; return 0; }
...but unfortunately I don't know what I'm doing.