Solved

Redirecting CLI output in a c++ QT app.

Posted on 2010-11-10
9
550 Views
Last Modified: 2012-05-10
HI Guys,
I'm trying to redirect all stdout to a text box in my QT 4 app.  I found the following code in a forum and other people said it worked.  My problem is that I'm new to c++ and cant figure out the following compiler error message.  Any input would be appriciated.

 error: ‘myRedirector’ was not declared in this scope
freeems_loader.cpp:29: error: no matching function for call to ‘StdRedirector<char, std::char_traits<char> >::StdRedirector(std::ostream&, <unresolved overloaded function type>, QTextBrowser*&)’
freeems_LoaderRedirector.h:29: note: candidates are: StdRedirector<Elem, Tr>::StdRedirector(std::ostream&, void (*)(const Elem*, std::streamsize, void*), void*) [with Elem = char, Tr = std::char_traits<char>]
freeems_LoaderRedirector.h:16: note:                 StdRedirector<char, std::char_traits<char> >::StdRedirector(const StdRedirector<char, std::char_traits<char> >&)
freeems_loader.cpp:33: warning: the address of ‘QTextStream& endl(QTextStream&)’ will always evaluate as ‘true’
make[1]: *** [debug/freeems_loader.o] Error 1
make: *** [debug] Error 2
#ifndef FREEEMS_LOADERREDIRECTOR_H_

#define FREEEMS_LOADERREDIRECTOR_H_





#endif /* FREEEMS_LOADERREDIRECTOR_H_ */



template< class Elem = char, class Tr = std::char_traits< Elem > >

  class StdRedirector : public std::basic_streambuf< Elem, Tr >

  {

      /**

        * Callback Function.

        */

    typedef void (*pfncb) ( const Elem*, std::streamsize _Count, void* pUsrData );



  public:

      /**

        * Constructor.

        * @param a_Stream the stream to redirect

        * @param a_Cb the callback function

        * @param a_pUsrData user data passed to callback

        */

    StdRedirector( std::ostream& a_Stream, pfncb a_Cb, void* a_pUsrData ) :

      m_Stream( a_Stream ),

      m_pCbFunc( a_Cb ),

      m_pUserData( a_pUsrData )

    {

        //redirect stream

      m_pBuf = m_Stream.rdbuf( this );

    };



      /**

        * Destructor.

        * Restores the original stream.

        */

    ~StdRedirector()

    {

      m_Stream.rdbuf( m_pBuf );

    }



      /**

        * Override xsputn and make it forward data to the callback function.

        */

    std::streamsize xsputn( const Elem* _Ptr, std::streamsize _Count )

    {

      m_pCbFunc( _Ptr, _Count, m_pUserData );

      return _Count;

    }



      /**

        * Override overflow and make it forward data to the callback function.

        */

    typename Tr::int_type overflow( typename Tr::int_type v )

    {

      Elem ch = Tr::to_char_type( v );

      m_pCbFunc( &ch, 1, m_pUserData );

      return Tr::not_eof( v );

    }



 protected:

    std::basic_ostream<Elem, Tr>& m_Stream;

    std::streambuf*               m_pBuf;

    pfncb                         m_pCbFunc;

    void*                         m_pUserData;

  };

#########################################################



#include "freeems_LoaderRedirector.h"



a function in my QT GUI class



void FreeEMS_Loader::redirectCLI()

{

	myRedirector = new StdRedirector <>(std::cout,   outCallBack, ui.textOutput);



	std::cout <<"Hello! We are ready" << endl;

}

Open in new window

0
Comment
Question by:sean-keys
  • 5
  • 4
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 34103331
Well, the declaration is missing and the call to 'new' also seems to be missing its template argument - did you mean the following?
void FreeEMS_Loader::redirectCLI()

{

	StdRedirector`* myRedirector = new StdRedirector <char>(std::cout,   outCallBack, ui.textOutput);



	std::cout <<"Hello! We are ready" << endl;

}

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 34103337
Ooops, typo, that should be
void FreeEMS_Loader::redirectCLI()

{

	StdRedirector* myRedirector = new StdRedirector <char>(std::cout,   outCallBack, ui.textOutput);



	std::cout <<"Hello! We are ready" << endl;

}

Open in new window

0
 

Author Comment

by:sean-keys
ID: 34103447
Now I get a different error, thanks for the fast response.

 missing template arguments before ‘*’ token
freeems_loader.cpp:29: error: ‘myRedirector’ was not declared in this scope
freeems_loader.cpp:29: error: no matching function for call to ‘StdRedirector<char, std::char_traits<char> >::StdRedirector(std::ostream&, <unresolved overloaded function type>, QTextBrowser*&)’
0
 
LVL 86

Expert Comment

by:jkr
ID: 34103595
Argh, my bad, not a good idea to mention the template argument and then omitting it again - try
void FreeEMS_Loader::redirectCLI()

{

	StdRedirector<char>* myRedirector = new StdRedirector <char>(std::cout,   outCallBack, ui.textOutput);



	std::cout <<"Hello! We are ready" << endl;

}

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:sean-keys
ID: 34103908
Getting closer, I apologize for my ignorance.    

 error: no matching function for call to ‘StdRedirector<char, std::char_traits<char> >::StdRedirector(std::ostream&, <unresolved overloaded function type>, QTextBrowser*&)’
freeems_LoaderRedirector.h:29: note: candidates are: StdRedirector<Elem, Tr>::StdRedirector(std::ostream&, void (*)(const Elem*, std::streamsize, void*), void*) [with Elem = char, Tr = std::char_traits<char>]
freeems_LoaderRedirector.h:16: note:                 StdRedirector<char, std::char_traits<char> >::StdRedirector(const StdRedirector<char, std::char_traits<char> >&)
freeems_loader.cpp:35: warning: the address of ‘QTextStream& endl(QTextStream&)’ will always evaluate as ‘true’
0
 
LVL 86

Expert Comment

by:jkr
ID: 34104093
How is 'outCallBack' declared?
0
 

Author Comment

by:sean-keys
ID: 34104187

void FreeEMS_Loader::outCallBack( const char* ptr, std::streamsize count, void* pTextBox )
{
  (void) count;
  QTextBrowser* p = static_cast< QTextBrowser* >( pTextBox );
  p->append( ptr );
}
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 34104430
Well, that seems to be the problem, you can't use a member function here - can you make that a static member and make the call look like
void FreeEMS_Loader::redirectCLI()

{

	StdRedirector<char>* myRedirector = new StdRedirector <char>(std::cout,   (StdRedirector<char>::pfncb) outCallBack, ui.textOutput);



	std::cout <<"Hello! We are ready" << endl;

}

Open in new window

0
 

Author Closing Comment

by:sean-keys
ID: 34106104
Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now