QT use of undeclared identifier 'cout'
在QT 5.12中直接使用cout将提示错误如下:
添加库 #include<iostream>,并将cout&end改为std::cout&std::endl
代码如下:
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 }
此时错误警告消失,但是仍无窗口输出;
在qt的工程文件(.pro文件)中加入以下代码:
CONFIG += console
代码如下:
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 +=
此时cout函数可正常运行
参考:
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
转载请注明出处:https://www.cnblogs.com/lei-zi/