关于vector类型的使用问题

fqy222425 2008-04-17 04:55:00
这是C++ primer中的一段代码,具体哪里有问题请高手指教一下

#include <iostream>

#include <vector>

using std::cout;
using std::endl;
using std::vector;



int main()
{
// empty vector
vector<int> ivec;
int val;
// give each element a new value
for (vector<int>::size_type ix = 0;
ix != 10; ++ix)
ivec.push_back(ix);

cout << "ivec.size: " << ivec.size() << endl; // prints 10

// reset the elements in the vector to zero
for (vector<int>::size_type ix = 0; ix != ivec.size(); ++ix)
ivec[ix] = 0;

// is there anything to print?
if (ivec.empty() == false) {
cout << "vector contents: " << endl;
// print each element separated by a newline
for (vector<int>::size_type ix = 0;
ix != ivec.size(); ++ix)
cout << ivec[ix] << endl;
}
return 0;
}



--------------------Configuration: init_vec - Win32 Debug--------------------
Compiling...
init_vec.cpp
e:\c++ primer题解\ms_files\3\init_vec.cpp(46) : error C2653: 'vector<int,class std::allocator<int> >' : is not a class or namespace name
e:\c++ primer题解\ms_files\3\init_vec.cpp(46) : error C2065: 'size_type' : undeclared identifier
e:\c++ primer题解\ms_files\3\init_vec.cpp(46) : error C2146: syntax error : missing ';' before identifier 'ix'
e:\c++ primer题解\ms_files\3\init_vec.cpp(46) : error C2065: 'ix' : undeclared identifier
e:\c++ primer题解\ms_files\3\init_vec.cpp(47) : error C2143: syntax error : missing ')' before '++'
e:\c++ primer题解\ms_files\3\init_vec.cpp(47) : warning C4552: '!=' : operator has no effect; expected operator with side-effect
e:\c++ primer题解\ms_files\3\init_vec.cpp(47) : error C2059: syntax error : ';'
e:\c++ primer题解\ms_files\3\init_vec.cpp(47) : error C2059: syntax error : ')'
e:\c++ primer题解\ms_files\3\init_vec.cpp(48) : error C2146: syntax error : missing ';' before identifier 'ivec'
e:\c++ primer题解\ms_files\3\init_vec.cpp(53) : error C2653: 'vector<int,class std::allocator<int> >' : is not a class or namespace name
e:\c++ primer题解\ms_files\3\init_vec.cpp(53) : error C2146: syntax error : missing ';' before identifier 'ix'
e:\c++ primer题解\ms_files\3\init_vec.cpp(53) : error C2143: syntax error : missing ')' before '++'
e:\c++ primer题解\ms_files\3\init_vec.cpp(53) : warning C4552: '!=' : operator has no effect; expected operator with side-effect
e:\c++ primer题解\ms_files\3\init_vec.cpp(53) : error C2059: syntax error : ';'
e:\c++ primer题解\ms_files\3\init_vec.cpp(53) : error C2059: syntax error : ')'
e:\c++ primer题解\ms_files\3\init_vec.cpp(54) : error C2146: syntax error : missing ';' before identifier 'ivec'
e:\c++ primer题解\ms_files\3\init_vec.cpp(60) : error C2653: 'vector<int,class std::allocator<int> >' : is not a class or namespace name
e:\c++ primer题解\ms_files\3\init_vec.cpp(60) : error C2146: syntax error : missing ';' before identifier 'ix'
e:\c++ primer题解\ms_files\3\init_vec.cpp(61) : error C2143: syntax error : missing ')' before '++'
e:\c++ primer题解\ms_files\3\init_vec.cpp(61) : warning C4552: '!=' : operator has no effect; expected operator with side-effect
e:\c++ primer题解\ms_files\3\init_vec.cpp(61) : error C2059: syntax error : ';'
e:\c++ primer题解\ms_files\3\init_vec.cpp(61) : error C2059: syntax error : ')'
e:\c++ primer题解\ms_files\3\init_vec.cpp(62) : error C2146: syntax error : missing ';' before identifier 'cout'
Error executing cl.exe.

init_vec.exe - 20 error(s), 3 warning(s)



其中e:\c++ primer题解\ms_files\3\init_vec.cpp(46) : error C2653: 'vector<int,class std::allocator<int> >' : is not a class or namespace name是怎么回事呢?
我见书里写的就是for (vector<int>::size_type ix = 0;
ix != 10; ++ix)
到底是怎么回事,是少了using指令吗?
...全文
305 11 打赏 收藏 举报
写回复
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
必成桂 2010-10-09
  • 打赏
  • 举报
回复
新版的编译器已经没有问题了。

c++的编译器不停的在支持新特性,这也是c++越来越难学的原因!
panshoup 2010-10-09
  • 打赏
  • 举报
回复
不知道LZ是用什么编译器弄得反正是没有命名空间 我也不清楚好像是那个SIZE_TYPE有错 你加个
using namespace std;的就可以了
还有vector <int>这里空格可以 我VC6.0能编译成功。。。
还有输出语句怎么是<_<这里怎么又空格呢。。。。

我把你改了下 在VC6.0能运行 结果我没看。。。就是有个警告你没用VAL变量。。。


#include <iostream>

#include <vector>

using std::cout;
using std::endl;
using std::vector;
using size_type;

int main()
{
// empty vector
vector <int> ivec;
int val;
// give each element a new value
for (vector<int>::size_type ix = 0;ix != 10; ++ix)
ivec.push_back(ix);

cout << "ivec.size: " << ivec.size() << endl; // prints 10

// reset the elements in the vector to zero
for (vector<int>::size_type ix1 = 0; ix1 != ivec.size(); ++ix1)
ivec[ix1] = 0;

// is there anything to print?
if (ivec.empty() == false) {
cout << "vector contents: " << endl;
// print each element separated by a newline
for (vector <int>::size_type ix = 0;
ix != ivec.size(); ++ix)
cout << ivec[ix] << endl;
}
return 0;
}


可能using namespace std;这样写效率不怎么搞
请高手解答。。。
fanguanggao 2010-10-09
  • 打赏
  • 举报
回复
楼主可以这样解决,先typedef std::vector<int>::size_type abc; 然后用abc来取代所有的vector <int>::size_type来定义指针,这样问题就可以解决了。
return0x0 2008-04-17
  • 打赏
  • 举报
回复
g++ 是一种 编译器
jieao111 2008-04-17
  • 打赏
  • 举报
回复
一种编译器,不是微软的。自己搜,迅雷应该有下的
fqy222425 2008-04-17
  • 打赏
  • 举报
回复
把vector <int>::size_type
改成:
std::vector <int>::size_type

问题得到解决,谢谢。
还有就是我现在用的vc6,g++是个什么啊
jieao111 2008-04-17
  • 打赏
  • 举报
回复
编译器问题
physacco 2008-04-17
  • 打赏
  • 举报
回复
程序没有任何问题,鉴定完毕。
另外你用的貌似是VC6的编译器吧,别用了,推荐用G++
return0x0 2008-04-17
  • 打赏
  • 举报
回复
运行了下(vc2008)楼主的代码,除了给出“warning C4101: “val”: 未引用的局部变量”这个警告信息,没有其他的错误。
(变量val 没有被赋值,也没有被使用)

楼主把 "vector_<int>" 改成 vector<int> 试试看?
hastings 2008-04-17
  • 打赏
  • 举报
回复
vector <int>::size_type
改成:
std::vector <int>::size_type
bargio_susie 2008-04-17
  • 打赏
  • 举报
回复
你用什么编译器运行的?
相关推荐
发帖
C++ 语言

6.3w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
帖子事件
创建了帖子
2008-04-17 04:55
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下