CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2009
    Posts
    326

    Creating function pointer to std::endl

    Hi,

    I am trying to create a function pointer to std::endl

    I get a compilation error in line number 36, where I am trying to assign the function pointer of std::endl to a pointer variable.

    The compilation error states that ios_base constructor is private, but I am not trying to execute std::endl I am just assing the pointer value.


    Pasted below is the code and compilation error:

    Code:
    /*
    
        Aim - To understand std :: endl
    
        std :: endl is actually a function
    
        
        template <class charT, class traits>
        basic_ostream<charT,traits>& endl ( basic_ostream<charT,traits>& os );
    
        template<
                  class charT,
                   class Traits = std::char_traits<charT>
                > class basic_ostream;
    
    
    */
    
    #include <iostream>
    #include <fstream>
    
    int main()
    {
    
        std :: ofstream fout;
    
        typedef std :: basic_ostream<std :: ofstream, std::char_traits<std :: ofstream> > BoType;
    
        std :: endl(fout);
    
    
        //Define function pointer type for std :: endl
    
        typedef BoType& (*EndlFunctType) (BoType&);
    
        EndlFunctType p1 = std :: endl;
    
        return(0);
    }

    Compilation error (I have pasted only the first few lines of the error):
    Code:
    /usr/include/c++/4.2.1/bits/ios_base.h: In copy constructor ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’:
    /usr/include/c++/4.2.1/iosfwd:55:   instantiated from ‘std::basic_ostream<_CharT, _Traits>& std::endl(std::basic_ostream<_CharT, _Traits>&) [with _CharT = std::basic_ofstream<char, std::char_traits<char> >, _Traits = std::char_traits<std::basic_ofstream<char, std::char_traits<char> > >]’
    test.cpp:36:   instantiated from here
    /usr/include/c++/4.2.1/bits/ios_base.h:779: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
    /usr/include/c++/4.2.1/iosfwd:55: error: within this context
    /usr/include/c++/4.2.1/iosfwd: In copy constructor ‘std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(const std::basic_ofstream<char, std::char_traits<char> >&)’:
    /usr/include/c++/4.2.1/iosfwd:92: note: synthesized method ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’ first required here 
    /usr/include/c++/4.2.1/streambuf: In copy constructor ‘std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)’:

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Creating function pointer to std::endl

    you're passing the ofstream type where the char type is required, ie. change "typedef std :: basic_ostream<std :: ofstream, std::char_traits<std :: ofstream> > BoType;" to "typedef std::basic_ostream< std:: ofstream::char_type, std:: ofstream::traits_type > BoType;"

  3. #3
    Join Date
    Feb 2009
    Posts
    326

    Re: Creating function pointer to std::endl

    Thank you so much !! I was breaking my head over it for a long time...

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured