Template stream operator overloading
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Template stream operator overloading

  1. #1
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Question Template stream operator overloading

    I want to be able to put the template class below in a cin and cout and thereby be able to input/output the value contained within. Has anyone done this before and know how it could be done?
    Code:
    template<class T> SomeClass
    {
    public:
        SomeClass(T inval) { value = inval; }
    protected:
        T value;
    }
    Example of what I want to be able to do:
    Code:
    SomeClass someObject<int>(5);
    cout << someObject << endl;
    When run, I want this code to output '5'. Thanks
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  2. #2
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Template stream operator overloading

    Quote Originally Posted by Etherous View Post
    Has anyone done this before and know how it could be done?
    Quite trivial to implement:

    Code:
    template<class T> class SomeClass
    {
    public:
        SomeClass(T inval) : value(inval) {} //This is real initialization.
    
        friend std::ostream& operator<<(std::ostream& outputStream, const SomeClass& instance)
        {
            return outputStream << instance.value;
        }
    
    protected:
        T value;
    };
    EDIT: Forgot about the second part of your question; it's the same idea with operator>>, but you'll be dealing with std::istream (in the case of std::cin).
    Last edited by Plasmator; March 22nd, 2009 at 08:48 PM.

  3. #3
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Template stream operator overloading

    Well, not surprisingly, it works. I feel stupid because it's such a simple thing to do. I just haven't needed to do this for quite some time. Thanks.
    Last edited by Etherous; March 22nd, 2009 at 11:10 PM.
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  4. #4
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Template stream operator overloading

    Is there any approach that works with any class including different variable ?

    This is tedious because you need to write this every time for every class.
    Thanks for your help.

  5. #5
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Template stream operator overloading

    Quote Originally Posted by Peter_APIIT View Post
    Is there any approach that works with any class including different variable ?

    This is tedious because you need to write this every time for every class.
    The compiler can't read your mind (yet).

  6. #6
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Template stream operator overloading

    I mean does generic programming able to handle such a tedious task for every class.
    Thanks for your help.

  7. #7
    Join Date
    Jul 2007
    Posts
    17

    Re: Template stream operator overloading

    I have a template class called basic_posint, in which I have declared a friend function

    Code:
    template<unsigned BASE>
    class base_posint : public integer {
        ...
    public:
        friend std::ostream& operator <<(std::ostream&, const basic_posint);
    }
    but the compiler gives me the following error:

    Code:
    integer.hpp:85: error: ISO C++ forbids declaration of `basic_posint' with no type
    The function is declared as public. What does it mean, doesn't it recognize basic_posint as a typename?

    Edit: I also tried your example template class, and it compiled without any warnings. On the other hand, if I replaced the function body by a semi colon (hence defining the function somewhere else), I got the following warning:

    Code:
    integer.hpp:48: warning: friend declaration `std::ostream& operator<<(std::ostream&, const SomeClass<T>&)' declares a non-template function
    integer.hpp:48: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
    Is it supposed to be like this? Do you have to define the function within the class to prevent this warning? (I'm compiling with g++)
    Last edited by TriKri; November 1st, 2009 at 05:20 PM.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,250

    Re: Template stream operator overloading

    Quote Originally Posted by TriKri
    I have a template class called basic_posint
    Your class template is named base_posint. You need to decide if you want to use base_posint or basic_posint.

    Quote Originally Posted by TriKri
    Do you have to define the function within the class to prevent this warning? (I'm compiling with g++)
    No. Read this FAQ on Why do I get linker errors when I use template friends? for a solution.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Jul 2007
    Posts
    17

    Re: Template stream operator overloading

    Quote Originally Posted by laserlight View Post
    Your class template is named base_posint. You need to decide if you want to use base_posint or basic_posint.


    No. Read this FAQ on Why do I get linker errors when I use template friends? for a solution.
    Doh! XD Sometimes I feel really stupid. I replaced basic_posint with base_posint, and the problem got solved. Strange, huh? :P

    Concerning the warnings I got from the compiler, I had to do both of the things that were mentioned in your link in order to get it to compile. If I remove the <> template indication I get a warning and then undefined reference to `operator<<(std::ostream&, base_posint<10u> const&)' and it won't compile, and if I remove the pre-declaration I get an error saying my friend function does not match any template declaration. But now it works just fine. Thanks!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center