Thread: Error declaring default argument to friend function

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Question Error declaring default argument to friend function

    I'm stumped. This only gives me an error if the class is a template and the functions are declared within the class. Here's a stripped-down example:

    Code:
    template < typename T >
    struct foo
    {
        friend void bar( foo< T > const& data, bool condition = false )
        {    }
        
        friend void qux( foo< T > const& data )
        {        
            bar( data );
        }
    };
    
    int main( void )
    {
        qux( foo< int >( ) );
        return 0;
    }
    The error doesn't make much sense either:

    "default argument given for parameter 2 of `void bar(const foo<int>&, bool)' after previous specification in `void bar(const foo<int>&, bool)'.

    Any ideas?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    OK, well, I think I've got it now. Seems as though friend functions (of templates) are supposed to be declared as templates themselves (even if within the class). I didn't think it was necessary since it usually compiles without complaint; I just figured that the compiler was deducing the parameter (given that it's declared within the class, after all). That would be the most logical thing to do, anyway. Go figure.
    Last edited by Sebastiani; 07-05-2009 at 01:55 AM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM