简单的程序无法使用 qtcreator 进行编译
Simple program won't compile with qtcreator
我安装了qt 4.7.4和gcc 4.6.1。我试着编译这个程序,但是它不能为我编译:
为什么我不能编译这段代码?
#include <QApplication>
#include <iostream>
using std::cout;
int main(int argc, char** argv)
{
QApplication app(argc,argv);
int a[] = {1,2};
for (auto e : a)
{
cout << e << 'n';
}
return app.exec();
}
错误:
C:…main.cpp:9:错误:'e'没有命名类型
for (auto e : a)
是c++11标准中基于范围的for循环。您需要在gcc中使用-std=c++0x命令行启用c++11
我这个作品(g + + 4.6.1, Qt 4.7.1):
g++ --std=c++0x -I$QTDIR/include/QtGui -I$QTDIR/include
test.cpp -L$QTDIR/lib -lQtCore -lQtGui
你需要——std=c++0x编译器标志
我的猜测是qtcreator(和qmake)没有给编译器提供指示它使用c++ 2011的标志。
首先,您要确保您的c++文件是用c++ 11方言编译的(即使用-std=c++0x标志到g++
),因为您使用auto
类型推断功能。
那么,我认为您的for
循环可能无效。也许你想让a
成为std::vector<int>