[RESOLVED] How come cout won't accept an object if it's been returned from a function?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] How come cout won't accept an object if it's been returned from a function?

  1. #1
    Join Date
    Sep 2009
    Posts
    10

    [RESOLVED] How come cout won't accept an object if it's been returned from a function?

    I'm making a fairly simple matrix class and it's mostly working so far but I've found a curiosity when sending it to cout.

    It seems like if I create a matrix4 object and then send it to cout, that works fine. But if I create the matrix4 as I send it to cout, then I get an error.

    If I uncomment the penultimate line of the code below, I get a huge error message. But I would have thought that it was equivalent to the line above it, which works fine.

    The get_transpose() function, the overloaded << operator and the start of the error message look like this:
    Code:
    matrix4 get_transpose() const;
    
    std::ostream& operator << ( std::ostream& stream, matrix4& m );  
    
    matrix4_test.cpp:26: error: no match for 'operator<<' in 'std::cout << matrix4::get_transpose() const()'
    Any ideas why it's going wrong or how I can fix it so that I can send matrix4s directly to cout the same as normal types? I've attached the relevant files in case they help.

    Here's the problem code, the penultimate line in particular:
    Code:
    #include <iostream>
    #include "matrix4.h"
    
    matrix4 get_transpose()	const;
    
    int main()
    {
        float a[4][4] = { 
    				0,	1,	2,	3,
    				4,	5,	6,	7,
    				8,	9,	10,	11,
    				12,	13,	14,	15
    			    };
    
        matrix4 mat2(a);
    	
        matrix4 transpose = mat2.get_transpose();
    	
        std::cout << mat2 << "\n\n";
        std::cout << transpose << "\n\n";
        //std::cout << mat2.get_transpose() << "\n\n";
    	
        return(0);
    }
    I'm using MinGW btw. Thanks for reading.
    Attached Files Attached Files

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: How come cout won't accept an object if it's been returned from a function?

    usually the operator << takes the opject by const reference
    e.g.
    Code:
    std::ostream& operator << ( std::ostream& stream, const matrix4& m );
    guess your error has to do that you call operator << on a temporary object. If that is true then changing the operator << should help.
    Kurt

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: How come cout won't accept an object if it's been returned from a function?

    Indeed, a temporary can be bound to a const reference but not to a non-const reference. Hence the trouble.

  4. #4
    Join Date
    Sep 2009
    Posts
    10

    Re: How come cout won't accept an object if it's been returned from a function?

    Thanks a lot that's sorted it out. Couldn't have hoped for clearer answers.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center