operator<< for templated subclass?

Discussion in 'C++' started by Guenther Brunthaler, Sep 30, 2003.

  1. Hi template specialists,

    I have a problem with the code listed below.

    What I wanted to do is defining an operator<< that is able to output
    a 'matrix<ELEMENT_T, INDEX_T>::subrange' object
    into a 'std::basic_ostream<charT, traits>'.

    For some reason, it does not work. The compiler always complains:

    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    te.cpp:
    Error E2094 test.cpp: 'operator<<' not implemented in type
    'std::eek:stream' for arguments of type 'matrix<double,unsigned
    int>::subrange' in function main(int,char * *)
    *** 1 errors in Compile ***

    The INTEL compiler gives a similar error.

    On the other hand, MSVC 7 compiles without any warnings.

    So - which of the compilers are correct?

    And does anyone have an idea what there might be wrong with the code?

    Note:

    If I change the code to output a
    'matrix<ELEMENT_T, INDEX_T>'
    instead of a
    'matrix<ELEMENT_T, INDEX_T>::subrange'
    then it works perfectly for all compilers!

    Only that this is not what I actually want to do.

    So, please help me if you possibly can!

    ---------snip-----------
    #include <iostream>
    #include <cstdlib>


    template<typename ELEMENT_T, typename INDEX_T>
    struct matrix {
    // I want to output an object of this nested type.
    struct subrange {};
    };

    // This templated global function will not be used
    // due to some reason unknown to me.
    template<
    typename ELEMENT_T, typename INDEX_T, typename charT, typename traits
    >

    std::basic_ostream<charT, traits> &operator<<(
    std::basic_ostream<charT, traits> &os,
    matrix<ELEMENT_T, INDEX_T>::subrange const &range)
    {
    os <<"matrix range";
    return os;
    }

    int main(int, char **) {
    matrix<double, size_t>::subrange x;
    std::cout << x;
    return EXIT_SUCCESS;
    }
    ---------snip-----------

    sincerly,

    Guenther
     
    Guenther Brunthaler, Sep 30, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jstorta
    Replies:
    3
    Views:
    476
    jstorta
    Feb 20, 2006
  2. Guenther Brunthaler

    operator<< for templated subclass?

    Guenther Brunthaler, Sep 30, 2003, in forum: C++
    Replies:
    4
    Views:
    495
    tom_usenet
    Oct 1, 2003
  3. S.Volkov
    Replies:
    2
    Views:
    242
    S.Volkov
    Mar 12, 2006
  4. Trans
    Replies:
    8
    Views:
    352
    Robert Klemme
    Oct 23, 2008
  5. Fab

    Subclass of subclass

    Fab, Aug 9, 2012, in forum: C++
    Replies:
    0
    Views:
    417
Loading...

Share This Page