如下代码产生如下错误:
test.cpp: In function `int main()':
test.cpp:15: error: cannot convert `__gnu_cxx::__normal_iterator<label**, std::vector<label*, std::allocator<label*> > >' to `label**' in initialization
#include
<
iostream
>
#include
<
vector
>
using
namespace
std;

struct
label

...
{
intx;
inty;
}
;

int
main()

...
{
vector<label*>plabel;
label** pCurrlabel= plabel.begin();
return0;
}
改为
label
*
pCurrlabel
= *
(plabel.begin());
就好了。
因为 plabel.begin()返回的是一个迭代器,并不能自然地转化为指针。
(摘自:www.linuxquestions.org)
test.cpp: In function `int main()':
test.cpp:15: error: cannot convert `__gnu_cxx::__normal_iterator<label**, std::vector<label*, std::allocator<label*> > >' to `label**' in initialization
#include
<
iostream
>
#include
<
vector
>
using
namespace
std;
struct
label
...
{
intx;
inty;
}
;
int
main()
...
{
vector<label*>plabel;
label** pCurrlabel= plabel.begin();
return0;
}
改为
label
*
pCurrlabel
= *
(plabel.begin());
因为 plabel.begin()返回的是一个迭代器,并不能自然地转化为指针。
(摘自:www.linuxquestions.org)
本文解决了一个C++编程中常见的问题:从std::vector的begin()方法获取迭代器并尝试将其直接赋值给指针变量时出现的类型转换错误。通过提供修改前后的代码示例,展示了正确的迭代器使用方式。
并非指针&spm=1001.2101.3001.5002&articleId=81442697&d=1&t=3&u=10d0bb236f844e028b860da4700fa362)
1626

被折叠的 条评论
为什么被折叠?



