g ++错误:"vec"未命名类型 [英] g++ error: ‘vec’ does not name a type
本文介绍了g ++错误:"vec"未命名类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用C ++ STL向量.这是我的程序:
I'm trying to work with C++ STL vectors. Here is my program:
# include <iostream>
# include <vector>
using namespace std;
vector<int> vec;
vec.push_back(10);
vec.push_back(5);
vec.push_back(1);
vector<int>::iterator itr1 = vec.begin();
vector<int>::iterator itr2 = vec.end();
for(vector<int>::iterator itr = itr1; itr != itr2; itr++){
cout << *itr << endl;
}
我收到错误 error:在
.我看不到我在做什么错. vec.push_back();
行上,"vec"没有命名类型
I get the error error: ‘vec’ does not name a type
on the lines vec.push_back();
. I'm unable to see what I'm doing wrong here.
推荐答案
仅在函数内部允许使用诸如 vec.push_back(42);
之类的语句.例如,
Statements such as vec.push_back(42);
are only allowed inside of functions. For example,
#include <iostream>
#include <vector>
int main()
{
using namespace std;
vector<int> vec;
vec.push_back(10);
vec.push_back(5);
vec.push_back(1);
vector<int>::iterator itr1 = vec.begin();
vector<int>::iterator itr2 = vec.end();
for(vector<int>::iterator itr = itr1; itr != itr2; itr++){
cout << *itr << endl;
}
}
这篇关于g ++错误:"vec"未命名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文