Tag Archives: QT use of undeclared identifier ‘cout’

QT use of undeclared identifier ‘cout’

Using cout directly in QT 5.12 will prompt an error as follows:

Add library?Include & lt; iostream>, And cout & amp; End is changed to STD:: cout & amp; std::endl

The code is as follows:

 1 #include "mainwindow.h"
 2 #include<iostream>
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10 
11     std::cout <<"hello world" << std::endl;
12 
13     return a.exec();
14 }

At this time, the error warning disappears, but there is still no window output

Add the following code to the project file (. Pro file) of QT:

CONFIG += console

The code is as follows:

 1 #-------------------------------------------------
 2 #
 3 # Project created by QtCreator 2019-09-05T16:30:11
 4 #
 5 #-------------------------------------------------
 6 
 7 QT       += core gui
 8 
 9 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10 
11 TARGET = VCICAN
12 TEMPLATE = app
13 
14 # The following define makes your compiler emit warnings if you use
15 # any feature of Qt which has been marked as deprecated (the exact warnings
16 # depend on your compiler). Please consult the documentation of the
17 # deprecated API in order to know how to port your code away from it.
18 DEFINES += QT_DEPRECATED_WARNINGS
19 
20 # You can also make your code fail to compile if you use deprecated APIs.
21 # In order to do so, uncomment the following line.
22 # You can also select to disable deprecated APIs only up to a certain version of Qt.
23 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
24 
25 CONFIG += c++11
26 CONFIG += console
27 
28 SOURCES += \
29         main.cpp \
30         mainwindow.cpp
31 
32 HEADERS += \
33         mainwindow.h
34 
35 FORMS += \
36         mainwindow.ui
37 
38 # Default rules for deployment.
39 qnx: target.path = /tmp/$${TARGET}/bin
40 else: unix:!android: target.path = /opt/$${TARGET}/bin
41 !isEmpty(target.path): INSTALLS += target
42 
43 DISTFILES +=

. a normal operation can be carried out at this time

References

https://blog.csdn.net/u014563989/article/details/44537727

https://zhidao.baidu.com/question/143715512.html

http://blog.sina.com.cn/s/blog_4f183d960100sdxf.html

http://www.qtcn.org/bbs/simple/?t21816.html