Error using cout in conjunction with a class method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • droors
    New Member
    • Oct 2006
    • 1

    #1

    Error using cout in conjunction with a class method

    During compilation, I get a slew of errors whenever I perform
    this operation:

    Code:
    cout << aMatrix.Transpose( ) << endl;
    -- the overloaded operator<< non-member method is:
    Code:
           ostream & operator<<( ostream & out, const Matrix & matrixRHS )
    -- the member method Transpose( ) [ transposes the calling object by
    moving values into a temp Matrix object and then assignin it to the
    calling object] is:

    Code:
         void Transpose( void );
    -- errors: [including quite a few more it won't let me post but they all
    deal with the << operator]
    Code:
    Test.cpp:32: error: no match for ‘operator<<’ in ‘std::cout << aMatrix. Matrix::Transpose()’
    /usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/bits/ostream.tcc:67: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/bits/ostream.tcc:78: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/bits/ostream.tcc:90: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
    /usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/bits/ostream.tcc:125: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]

    However, the code functions correctly with this set of commands:

    Code:
    aMatrix.Transpose( ) ;
    cout << aMatrix << endl;
    Does anyone have any idea as to why the first method produces so many errors concerning the ostream << operator? I thought it may have something to do with the const-ness of the method but I am not allowed to change the Transpose( ) method so that it returns a const Matrix object. Anybody have any input?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    As well as the normal operations available you have added an overload for the cout << operator to accept a const Matrix &.

    However the Transpose function is defined as

    void Matrix::Transpo se( void );

    so in this line of code

    cout << aMatrix.Transpo se( ) << endl;

    you are passing the type void to cout <<. Since there is no override for void an(or several) error ensues.

    However in this code

    aMatrix.Transpo se( )
    cout << aMatrix << endl;

    You call the tranpose function and then pass the matrix to cout << and it can use the overloaded operator you have defined.

    Comment

    Working...