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. Using streams

Using streams

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 Posts 3 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.
  • B Offline
    B Offline
    Berlus
    wrote on last edited by
    #1

    Hello, I need help in converting these two function into one function (since basicaly it is the same code). Does anoyone have an idea ?

    struct File_Writer
    {
    File_Writer(ofstream& output) : m_output(output) { ; }
    void operator(MyStruct& ms)
    {
    output << ms.a << endl;
    output << ms.b << endl;
    }
    ofstream& m_output; };

    struct Display_Writer
    {
    void operator(MyStruct& ms)
    {
    cout << " a: " << ms.a << endl;
    cout << " b: " << ms.b << endl;
    }
    };

    Thanks, Berlus

    D S 2 Replies Last reply
    0
    • B Berlus

      Hello, I need help in converting these two function into one function (since basicaly it is the same code). Does anoyone have an idea ?

      struct File_Writer
      {
      File_Writer(ofstream& output) : m_output(output) { ; }
      void operator(MyStruct& ms)
      {
      output << ms.a << endl;
      output << ms.b << endl;
      }
      ofstream& m_output; };

      struct Display_Writer
      {
      void operator(MyStruct& ms)
      {
      cout << " a: " << ms.a << endl;
      cout << " b: " << ms.b << endl;
      }
      };

      Thanks, Berlus

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Berlus wrote:

      void operator(MyStruct& ms)

      You first need to show code that actually compiles.

      Berlus wrote:

      I need help in converting these two function into one function (since basicaly it is the same code).

      Since they are outputting to two unrelated streams, what exactly is it you think can be merged?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

      1 Reply Last reply
      0
      • B Berlus

        Hello, I need help in converting these two function into one function (since basicaly it is the same code). Does anoyone have an idea ?

        struct File_Writer
        {
        File_Writer(ofstream& output) : m_output(output) { ; }
        void operator(MyStruct& ms)
        {
        output << ms.a << endl;
        output << ms.b << endl;
        }
        ofstream& m_output; };

        struct Display_Writer
        {
        void operator(MyStruct& ms)
        {
        cout << " a: " << ms.a << endl;
        cout << " b: " << ms.b << endl;
        }
        };

        Thanks, Berlus

        S Offline
        S Offline
        Stefan_Lang
        wrote on last edited by
        #3

        Since all output stream classes inherit from ostream, you can do this:

        using std;
        struct MyStruct {
        int a; // just an example
        double b; // just an example
        //...
        };
        // this implements the streaming operator for all classes derived from std::ostream
        ostream& operator<<(ostream& output, MyStruct& ms) {
        output << " a: " << ms.a << endl;
        output << " b: " << ms.b << endl;
        return output;
        }

        int main () {
        MyStruct ms;
        ms.a = 3;
        ms.b = 3.1415926;
        ofstream myfile("test.txt");
        myfile << ms; // writes to file
        cout << ms; // writes to console
        }

        [edit]fixed implementation[/edit]

        modified on Thursday, May 26, 2011 9:31 AM

        B 1 Reply Last reply
        0
        • S Stefan_Lang

          Since all output stream classes inherit from ostream, you can do this:

          using std;
          struct MyStruct {
          int a; // just an example
          double b; // just an example
          //...
          };
          // this implements the streaming operator for all classes derived from std::ostream
          ostream& operator<<(ostream& output, MyStruct& ms) {
          output << " a: " << ms.a << endl;
          output << " b: " << ms.b << endl;
          return output;
          }

          int main () {
          MyStruct ms;
          ms.a = 3;
          ms.b = 3.1415926;
          ofstream myfile("test.txt");
          myfile << ms; // writes to file
          cout << ms; // writes to console
          }

          [edit]fixed implementation[/edit]

          modified on Thursday, May 26, 2011 9:31 AM

          B Offline
          B Offline
          Berlus
          wrote on last edited by
          #4

          Thanks for the quick response. Is there a way to insert the "serialization" code into the struct itself ? something like:

          #include <iostream>
          #include <fstream>
          using namespace std;

          struct MyStruct
          {
          ostream& operator<<(ostream& output) {
          output << " a: " << a << endl;
          output << " b: " << b << endl;
          return output;
          }

          int a;
          double b;
          

          };

          int main(void)
          {
          MyStruct ms;
          ms.a = 5;
          ms.b = 4.2;

          cout << ms;

          // Write to a file without changing methods in MyStruct
          ofstream my_file("me.txt");
          my_file << ms;

          return 0;
          }

          I tried it, and got compilation errors. IntelliSense: no operator "<<" matches these operands I am trying to create some sort of a generic ToString. Thanks, Berlus

          S 1 Reply Last reply
          0
          • B Berlus

            Thanks for the quick response. Is there a way to insert the "serialization" code into the struct itself ? something like:

            #include <iostream>
            #include <fstream>
            using namespace std;

            struct MyStruct
            {
            ostream& operator<<(ostream& output) {
            output << " a: " << a << endl;
            output << " b: " << b << endl;
            return output;
            }

            int a;
            double b;
            

            };

            int main(void)
            {
            MyStruct ms;
            ms.a = 5;
            ms.b = 4.2;

            cout << ms;

            // Write to a file without changing methods in MyStruct
            ofstream my_file("me.txt");
            my_file << ms;

            return 0;
            }

            I tried it, and got compilation errors. IntelliSense: no operator "<<" matches these operands I am trying to create some sort of a generic ToString. Thanks, Berlus

            S Offline
            S Offline
            Stefan_Lang
            wrote on last edited by
            #5

            Hehe, that's what I first did, and hence my edit. :) Unfortunately the streaming operators expect the type to be streamed as second parameter. If it were the first parameter, then your code would work, as in class methods the instance of the class itself is always passed as a hidden parameter. So, what you can do, is overriding the subtraction operator like in either of these ways:

            struct MyStruct {
            int a;
            MyStruct(int value) : a(value) {} // conversion constructor, for implicit casting of int to MyStruct
            MyStruct operator-(const MyStruct& op2) {
            return (a-op2.a);
            }
            };

            or

            struct MyStruct {
            int a;
            MyStruct(int value) : a(vlaue) {}
            };
            MyStruct operator-(const MyStruct& op1, const MyStruct& op2) {
            return op1.a - op2.a;
            }

            Both implementations are equivalent. The only difference is that the first variant skips the first parameter of the second variant, because the instance will be used in its stead.

            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