一条语句放在for循环的内外,放在里面报错

陈思有 2012-03-06 05:49:41
#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> ivec;
int a;

while (cin >> a)
{
ivec.push_back(a);
}

int *p = new int[ivec.size()];
int *q = p;//这条语句如果放在for循环里面会出错,放在外面就没事???
for(vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++, q++)
{
*q = *iter;
}

for (size_t i = 0; i != ivec.size(); i++)
{
cout << *(p + i) << endl;
}

delete [] p;

return 0;
}


F:\C++primer练习\第四章\28.cpp(34) : warning C4518: 'int ' : storage-class or type specifier(s) unexpected here; ignored
F:\C++primer练习\第四章\28.cpp(34) : error C2440: 'initializing' : cannot convert from 'int *' to 'int ** '
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
F:\C++primer练习\第四章\28.cpp(34) : warning C4228: nonstandard extension used : qualifiers after comma in declarator list are ignored
F:\C++primer练习\第四章\28.cpp(34) : error C2146: syntax error : missing ')' before identifier 'iter'
F:\C++primer练习\第四章\28.cpp(34) : warning C4552: '!=' : operator has no effect; expected operator with side-effect
F:\C++primer练习\第四章\28.cpp(34) : error C2059: syntax error : ';'
F:\C++primer练习\第四章\28.cpp(34) : error C2059: syntax error : ')'
F:\C++primer练习\第四章\28.cpp(35) : error C2143: syntax error : missing ';' before '{'
F:\C++primer练习\第四章\28.cpp(36) : error C2440: '=' : cannot convert from 'int' to 'int *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
执行 cl.exe 时出错.

28.obj - 1 error(s), 0 warning(s)
...全文
171 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
陈思有 2012-03-06
  • 打赏
  • 举报
回复
我傻了,汗
ppsharp 2012-03-06
  • 打赏
  • 举报
回复
C++ Primer 英文影印版 第210页
从头来过 2012-03-06
  • 打赏
  • 举报
回复
放到for里面的话 ,你for中q++,q还没定义就使用了
ppsharp 2012-03-06
  • 打赏
  • 举报
回复
int *q = p;
vector<int>::iterator iter = ivec.begin()

这是两个不同类型的初始化,
for循环里面只能放同一类型的初始化。

比如说int i = 0, *a = & i;

这里只有一个分号,你怎么可以放两个不同类型的初始化呢,也放不进去
qixing1115 2012-03-06
  • 打赏
  • 举报
回复
for(vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); iter++, q++)
看标红的,你还没声明就用了

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧