Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Overloading << operator?

Overloading << operator?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
14 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cristoff
    wrote on last edited by
    #1

    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!

    T E A C 4 Replies Last reply
    0
    • C Cristoff

      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!

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      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 a cout << data and a file << 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]

      C 1 Reply Last reply
      0
      • T toxcct

        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 a cout << data and a file << 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]

        C Offline
        C Offline
        Cristoff
        wrote on last edited by
        #3

        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.

        T S 2 Replies Last reply
        0
        • C Cristoff

          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.

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          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]

          C 1 Reply Last reply
          0
          • C Cristoff

            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.

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #5

            I don't understand how varargs fits into overloading << ? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            1 Reply Last reply
            0
            • C Cristoff

              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!

              E Offline
              E Offline
              Eytukan
              wrote on last edited by
              #6

              [Message Deleted]

              T 1 Reply Last reply
              0
              • E Eytukan

                [Message Deleted]

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #7

                VivekuniQ wrote:

                is it really possible to overload a ">>" ??

                this is not what we're talking about, but to answer your prompt question, yes it is !!


                TOXCCT >>> GEII power
                [toxcct][VisualCalc 2.20]

                1 Reply Last reply
                0
                • T toxcct

                  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]

                  C Offline
                  C Offline
                  Cristoff
                  wrote on last edited by
                  #8

                  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.

                  T 1 Reply Last reply
                  0
                  • C Cristoff

                    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.

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #9

                    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]

                    C 1 Reply Last reply
                    0
                    • T toxcct

                      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]

                      C Offline
                      C Offline
                      Cristoff
                      wrote on last edited by
                      #10

                      Thank you very much for wanting to help me. The problem is that I've deleted it yesterday and I cannot find it now, although I'm searching it yet for my own needs. If I find it, I'll post it later. Thank you!

                      1 Reply Last reply
                      0
                      • C Cristoff

                        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!

                        A Offline
                        A Offline
                        Amol Ravatale
                        wrote on last edited by
                        #11

                        #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<

                        A 1 Reply Last reply
                        0
                        • A Amol Ravatale

                          #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<

                          A Offline
                          A Offline
                          Amol Ravatale
                          wrote on last edited by
                          #12

                          sorry , include these files #include "stdafx.h" #include #include #include #include #include #include #include #include

                          1 Reply Last reply
                          0
                          • C Cristoff

                            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!

                            C Offline
                            C Offline
                            Cristoff
                            wrote on last edited by
                            #13

                            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.

                            C 1 Reply Last reply
                            0
                            • C Cristoff

                              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.

                              C Offline
                              C Offline
                              Cristoff
                              wrote on last edited by
                              #14

                              It seems that CP likes eating #includes... of course I use: fstream and iostream.

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups