Output overloading

This is a discussion on Output overloading within the C++ Programming forums, part of the General Programming Boards category; I came up with a contradiction between the rules for overloading the output operator and what my code allowed me ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581

    Output overloading

    I came up with a contradiction between the rules for overloading the output operator and what my code allowed me to do when I tried to provoque the error.

    The class declares the operator unfriendly:
    Code:
    class Opers {
    public:
    /* ... */
        std::ostream& operator<<(std::ostream&, const Opers&);
    private:
    /*... */
    };
    The cpp file for the class goes on defining it:
    Code:
    #include "oper.h"
    std::ostream& operator<<(std::ostream &os, const Opers &obj) {
        os << obj.val() << " - " << obj.text();
        return os;
    }
    No errors, no warnings and the test in main() outputs correctly. This shouldn't happen from what I've been learning. The operator, being declared a member of Oper, should have the implicit This as the first parameter...

    What am I missing?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    The operator, being declared a member of Oper, should have the implicit This as the first parameter...
    Since operator<< is dyadic, an implicit this cannot be.

    Have you actually tried to use the function?

    My guess is that the operator<< member function is simply an error, but since it was never used, it never was a problem.

    The actual operator<< free function works because it doesnt need to be a friend function. However, since it is declared in the source file, you would not be able to use it since a file that includes oper.h would not have its prototype.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    I'm not sure if I understand Laserlight.

    I have it prototyped in the class, much like I would have any other function member that is later defined on the cpp file.

    The text in main is called with a simple std::cout << obj; (where obj is an object of Opers type). And it gave me the correct output.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    class Opers {
    public:
    /* ... */
        std::ostream& operator<<(std::ostream&, const Opers&);
    /*
     Error E2080: 'Opers::operator <<(std::ostream &,const Opers &)' 
     must be declared with one parameter
    */
    private:
    /*... */
    };
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Wait... I can see...

    My function definition is also a declaration.
    I didn't use the scope operator. I can understand what you mean by free function. The following does give me an error now.

    Code:
    #include "oper.h"
    std::ostream& Opers::operator<<(std::ostream &os, const Opers &obj) {
        os << obj.val() << " - " << obj.text();
        return os;
    }
    Only question now is why did I have access to that function before?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    I have it prototyped in the class, much like I would have any other function member that is later defined on the cpp file.
    But there is no operator<< that takes three arguments. operator<< is dyadic.

    The text in main is called with a simple std::cout << obj; (where obj is an object of Opers type). And it gave me the correct output.
    Amazing.

    Let's try a test:

    1. oper.h
    Code:
    #ifndef OPER_H
    #define OPER_H
    
    #include <iosfwd>
    
    class Opers {
    public:
    	std::ostream& operator<<(std::ostream&, const Opers&);
    	int val() const { return 0; }
    	int text() const { return 0; }
    };
    
    #endif
    2. oper.cpp
    Code:
    #include "oper.h"
    
    #include <ostream>
    
    std::ostream& operator<<(std::ostream& os, const Opers& obj) {
    	os << obj.val() << " - " << obj.text();
    	return os;
    }
    3. main.cpp
    Code:
    #include <iostream>
    
    #include "oper.h"
    
    int main() {
    	Opers test;
    	std::cout << test << std::endl;
    }
    What do you get?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I believe this is just a separate function.
    Code:
    std::ostream& operator<<(std::ostream &os, const Opers &obj) {
        os << obj.val() << " - " << obj.text();
        return os;
    }
    Code:
    #include <iostream>
    
    class Opers {
    };
    
    std::ostream& operator<< (std::ostream &os, const Opers &obj) {
        os << "hello world";
        return os;
    }
    
    int main() {
       Opers op;
       std::cout << op << '\n';
        return 0;
    }
    
    /* my output
    hello world
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    Only question now is why did I have access to that function before?
    You never did, since it was in a source file. Perhaps some build quirk? Did you remember to rebuild all to check?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Ah! The Rebuild All did it. Thanks.

    Now I get the proper errors. Trying to declare operator<< with more than one argument inside the class and a no match error from trying to access the operator<< from main.

    My first time where this strategy to provoque errors to better understand what I'm learning, backfires on me.


    Thanks both again
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Trying not to start another thread on a related issue...

    It seems I'm not being able to understand correctly how to use the overload input operator.
    The following isn't working. The error states a no match for operator>> on the line colored red.

    Code:
    ------- Opers.h ------
    class Opers {
    public:
    /* ... */
        friend std::istream& operator>>(std::istream&, const Opers&);
    private:
        int val_;
        std::string str_;
    };
    
    ------- Opers.cpp ------
    /* ... */
    std::istream& operator>>(std::istream &is, const Opers &obj) {
        is >> obj.val_ >> obj.str_;
        if(!is)
            obj = Opers();
        return is;
    }
    The idea is to read two values from the stream. What am I missing?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    Terribly sorry. The second parameter can't be const. The error message was too cryptic for me to look at the obvious.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. Overloading operator++
    By vopo in forum C++ Programming
    Replies: 9
    Last Post: 08-18-2008, 03:52 AM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 06:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21