简体   繁体   中英

How to initialize stdout stdin with Qt 5.7

I noticed something with qDebug() QTextStrean and generally stdin, stdout wanna ask, how it works actually, see this:

THIS WORKS!

method showmenu() using QTextStream

showMenu(){
            QTextStream m_out(stdout);
            QTextStream m_in(stdin);

            m_out() << "Hey";
}

THIS DOESN'T WORK!

.h

//declaration 

public:
   QTextStream m_out;
   QTextStream m_in;

.cpp

//method showMenu() 

showMenu(){
             m_out(stdout);
             m_in(stdin);

             m_out() << "Hey";
}

I noticed, it has problem with overloading, because also qDebug() uses stdout... am I correct?

It throws this error:

1>D:..\\App_console.cpp(20,15): error : no match for call to '(QTextStream) (_IO_FILE*&)'

I have included cstdio

What could it be?

Pre C++11, You will need to do that in your Constructor Initialization List . In the Constructor Definition of your class, say MyStreamer , you can initialize it like this:

class MyStreamer{
....
public:
   QTextStream m_out;
   QTextStream m_in;
};

In your .cpp file:

MyStreamer::MyStreamer(...) : m_out(stdout), m_in(stdin) {
    ....
}

In C++11 and beyond, you could simply use uniform initialization :

class MyStreamer{
....
public:
   QTextStream m_out{stdout};
   QTextStream m_in{stdin};
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM