执行C++ 11我有当我执行在终端的Ubuntu条命令“做”的问题。 我的生成文件的代码是:如何使用makefile文件
all: temp p1
%: %.cc g++ -lm -lcrypt -O2 -std=c++11 -pipe $< -o [email protected]
当然,我的文件是temp.cc和p1.cc,但我的问题是在p1.cc,其中的代码是:
#include <bits/stdc++.h>
using namespace std;
int main(){
vector<int> vec = {4,6,8,9,8,7,1,3,4,5,0,1};
for(auto i : vec)
cout<<i<<" ";
cout<<endl;
return 0;}
我使用 '使' 错误是:
[email protected]:~/P$ make
g++ p1.cc -o p1
p1.cc: In function ‘int main()’:
p1.cc:7:44: error: in C++98 ‘vec’ must be initialized by constructor, not by ‘{...}’
vector<int> vec = {4,6,8,9,8,7,1,3,4,5,0,1};
^
p1.cc:7:44: error: could not convert ‘{4, 6, 8, 9, 8, 7, 1, 3, 4, 5, 0, 1}’ from ‘<brace-enclosed initializer list>’ to ‘std::vector<int>’
p1.cc:9:11: error: ‘i’ does not name a type
for(auto i : vec)
^
p1.cc:11:2: error: expected ‘;’ before ‘cout’
cout<<endl;
^
p1.cc:12:2: error: expected primary-expression before ‘return’
return 0;
^
p1.cc:12:2: error: expected ‘)’ before ‘return’
make: *** [p1] Error 1
使用下一个命令行,编译:
g++ --std=c++11 p1.cc -o p1
和执行是好的:
[email protected]:~/P$ ./p1
4 6 8 9 8 7 1 3 4 5 0 1
请帮助我,我不明白为什么有问题,感谢您的支持:)
你缺少'G ++'前一个换行符在你的Makefile,通常,您链接到图书馆应该来的'G ++'命令的结束。而且,出于某种原因,'g ++'没有发现你的'-std = C++ 11'标志或者它不工作。 –
另外,你还没有'#include'd'vector'。 –
使用“#include”我得到了所有库,这是使用g ++编译器的一个优点,但是感谢您的帮助。 –
Rik4chu