鱼C论坛

 找回密码
 立即注册
查看: 1526|回复: 19

[已解决]迭代器错误 :vector iterators incompatible

[复制链接]
发表于 2019-6-24 18:48:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 VOGHOST 于 2019-6-24 20:25 编辑

期末作业遇到问题,有没有大佬帮忙看一下
这是简化后的代码
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

class A
{
protected:
        int a=5;
};
class B :public A
{
public:
        void SetA(int i) { a = i; }
        int GetA() { return a; }
};


class C
{
protected:
        int c=0;
        vector<A> a;
public:
        void SetC(int i){        c = i;}
        int GetC()        {        return c;}
};
void insert(vector<B>& VB, vector<C>& VC)
{
        C c;
        c.SetC(5);
        vector<B>::iterator ptr_b = VB.begin();
        vector<C>::iterator ptr_c = VC.begin();
        for (ptr_b; ptr_b < VB.end(); ptr_b++)
        {
                for (ptr_c; ptr_c < VC.end(); ptr_c++)  //这里刚刚出现错误,手误打错了
                        if ((*ptr_b).GetA() == (*ptr_c).GetC())
                                break;
                if (ptr_c == VC.end())
                {
                        c.SetC((*ptr_b).GetA());
                        VC.push_back(c);
                }
        }
}
void fun(vector<B> &vb)
{
        vector<C> vc;
        insert(vb, vc);
        vector<C>::iterator ptr_c = vc.begin();
        for (ptr_c; ptr_c < vc.begin(); ptr_c++)
                cout << (*ptr_c).GetC() << endl;
}
int main()
{
        B b;
        vector<B> v_b;
        for (int i = 0; i < 5; i++)
        {
                b.SetA(i);
                v_b.push_back(b);
        }
        fun(v_b);
        cout << "done2";
        return 0;
}

类之间的关系:
demo.png

错误
demo.png


报出上面图片的错误,我想问下我是错在了哪里以及这个问题的原因是什么?

------------------------------刚刚上面打错了-------------------
上面注释的应该为
for (ptr_c; ptr_c < VC.end(); ptr_c++)
但是迭代器的错误还是没有解决
最佳答案
2019-6-24 21:16:06
newu 发表于 2019-6-24 21:08
不理解了,VC不是传进来一个吗?


报错
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

class A
{
protected:
        int a = 5;
};

class B :public A
{
public:
        void SetA(int i) { a = i; }
        int GetA() { return a; }
};

class C
{
protected:
        int c = 0;
        vector<A> a;
public:
        void SetC(int i){        c = i;}
        int GetC()        {        return c;}
};

void insert(vector<B>& VB, vector<C>& VC)
{
        C c;
        c.SetC(5);
        vector<B>::iterator ptr_b = VB.begin();
        vector<C>::iterator ptr_c = VC.begin();
        for (ptr_b; ptr_b < VB.end(); ptr_b++)
        {
                for (ptr_c; ptr_c < VC.end(); ptr_c++)  //这里刚刚出现错误,手误打错了
                        if ((*ptr_b).GetA() == (*ptr_c).GetC())
                                break;
                if (ptr_c == VC.end())
                {
                        c.SetC((*ptr_b).GetA());
                        VC.push_back(c);
                }
        }
}

int main()
{
        B b;
        vector<B> vb;
        vector<C> vc;
        for (int i = 0; i < 5; i++)
        {
                b.SetA(i);
                vb.push_back(b);
        }

        insert(vb, vc);
        return 0;
}



不报错
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

class A
{
protected:
        int a = 5;
};

class B :public A
{
public:
        void SetA(int i) { a = i; }
        int GetA() { return a; }
};

class C
{
protected:
        int c = 0;
        vector<A> a;
public:
        void SetC(int i){        c = i;}
        int GetC()        {        return c;}
};

void insert(vector<B>& VB, vector<C>& VC)
{
        C c;
        c.SetC(5);
        vector<B>::iterator ptr_b = VB.begin();
        for (ptr_b; ptr_b < VB.end(); ptr_b++)
        {
                vector<C>::iterator ptr_c = VC.begin();
                for (ptr_c; ptr_c < VC.end(); ptr_c++)  //这里刚刚出现错误,手误打错了
                        if ((*ptr_b).GetA() == (*ptr_c).GetC())
                                break;
                if (ptr_c == VC.end())
                {
                        c.SetC((*ptr_b).GetA());
                        VC.push_back(c);
                }
        }
}

int main()
{
        B b;
        vector<B> vb;
        vector<C> vc;
        for (int i = 0; i < 5; i++)
        {
                b.SetA(i);
                vb.push_back(b);
        }

        insert(vb, vc);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-24 19:32:11 | 显示全部楼层
道行太浅了,没看懂你的意图。

调出来是这一步报错了
for (; ptr_c < VC.begin(); ptr_c++)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-24 19:38:36 | 显示全部楼层
newu 发表于 2019-6-24 19:32
道行太浅了,没看懂你的意图。

调出来是这一步报错了

原来代码太长了,简化了代码,所以就看不出意图了
不过不用在意这些
但是我是定位到42行出了错误,VC.push_back(c)出了错误,但就是找不到原因
能帮忙看一下是什么错误吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-24 19:58:34 | 显示全部楼层
VOGHOST 发表于 2019-6-24 19:38
原来代码太长了,简化了代码,所以就看不出意图了
不过不用在意这些
但是我是定位到42行出了 ...

我怎么测的是第36行出错的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-24 20:21:20 | 显示全部楼层
newu 发表于 2019-6-24 19:58
我怎么测的是第36行出错的

谢谢了,36这里确实有个错误,应该是
for (ptr_c; ptr_c < VC.end(); ptr_c++)

我改了后还是同样的错误
所以主要的错误没有解决,我把42行
VC.push_back(c);
注释掉就可以正常运行了
我觉得主要问题处在42行
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-24 20:30:35 | 显示全部楼层
VOGHOST 发表于 2019-6-24 20:21
谢谢了,36这里确实有个错误,应该是,
我改了后还是同样的错误
所以主要的错误没有解决,我把42行注释 ...

对的,我用VS2012测的第36行始终过不去,VC.push_back()是不是VC没有空间啊?

也就是vector<C> vc; 这句,是不是得给些空间?改为:vector<C> vc(10);
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-24 20:41:20 | 显示全部楼层
newu 发表于 2019-6-24 20:30
对的,我用VS2012测的第36行始终过不去,VC.push_back()是不是VC没有空间啊?

也就是vector vc; 这句 ...

这里我给了空间还是一样的错误
我想问下你vs2012是编译过不了,还是运行中报错
我编译可以过,但就是运行中出错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-24 20:43:32 | 显示全部楼层
VOGHOST 发表于 2019-6-24 20:41
这里我给了空间还是一样的错误
我想问下你vs2012是编译过不了,还是运行中报错
我编译可以过,但就是运 ...

和你截图给的错误是一样的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-24 20:47:45 | 显示全部楼层
我只知道地方,找不到原因呀,百度了这个报错,有各式各样的原因,就是没有和我相关的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-24 20:52:16 | 显示全部楼层
我看不懂你的代码,只是一些明显的错误,我改了
你的代码要实现什么?
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

class A
{
protected:
        int a = 5;
};

class B :public A
{
public:
        void SetA(int i) { a = i; }
        int GetA() { return a; }
};

class C
{
protected:
        int c = 0;
        vector<A> a;
public:
        void SetC(int i){        c = i;}
        int GetC()        {        return c;}
};

void insert(vector<B> &VB, vector<C> &VC)
{
        C c;
        c.SetC(5);
        //vector<B>::iterator ptr_b = VB.begin();
        //vector<C>::iterator ptr_c = VC.begin();
        //for (ptr_b; ptr_b < VB.end(); ptr_b++)
        for (auto ptr_b = VB.begin(); ptr_b != VB.end(); ptr_b++)
        {
                //for (ptr_c; ptr_c < VC.end(); ptr_c++)  //这里刚刚出现错误,手误打错了
                auto ptr_c = VC.begin();
                for (; ptr_c != VC.end(); ptr_c++)  //这里刚刚出现错误,手误打错了
                        if ((*ptr_b).GetA() == (*ptr_c).GetC())
                                break;
                if (ptr_c == VC.end())
                {
                        c.SetC((*ptr_b).GetA());
                        VC.push_back(c);
                }
        }
}
void fun(vector<B> &vb)
{
        vector<C> vc;
        insert(vb, vc);
        vector<C>::iterator ptr_c = vc.begin();
        for (; ptr_c != vc.end(); ptr_c++)
                cout << (*ptr_c).GetC() << endl;
}
int main()
{
        B b;
        vector<B> v_b;
        for (int i = 0; i < 5; i++)
        {
                b.SetA(i);
                v_b.push_back(b);
        }
        fun(v_b);
        cout << "done2";
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-24 20:57:00 | 显示全部楼层
newu 发表于 2019-6-24 20:30
对的,我用VS2012测的第36行始终过不去,VC.push_back()是不是VC没有空间啊?

也就是vector vc; 这句 ...
void insert(vector<B>& VB, vector<C>& VC)
{
        C c;
        c.SetC(5);
        vector<B>::iterator ptr_b = VB.begin();
        vector<C>::iterator ptr_c = VC.begin();
        for (ptr_b; ptr_b < VB.end(); ptr_b++)
        {
                for (ptr_c; ptr_c < VC.end(); ptr_c++)  //这里刚刚出现错误,手误打错了
                        if ((*ptr_b).GetA() == (*ptr_c).GetC())
                                break;
                if (ptr_c == VC.end())
                {
                        c.SetC((*ptr_b).GetA());
                        VC.push_back(c);
                }
        }
}


第6行定义了变量ptr_c,并且ptr_c为VC.begin(),因为VC为空,所以ptr_c == VC.begin() == VC.end()
之后ptr_c就一直指向VC.end(),不在改变
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-24 21:03:31 | 显示全部楼层
for (ptr_c; ptr_c < VC.end(); ptr_c++)

我找不到这种写法,不知道会不会出现问题


更通用的写法是这样,用 !=
for (ptr_c; ptr_c != VC.end(); ptr_c++)

我是这样写
for (auto iter = VC.begin(); iter != VC.end(); ++iter)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-24 21:07:01 | 显示全部楼层
人造人 发表于 2019-6-24 20:52
我看不懂你的代码,只是一些明显的错误,我改了
你的代码要实现什么?

谢谢,报错是没了,但是我还是想了解错误的原因
不是很懂用 auto ptr;和我用vector<B>::iterator ptr;有什么区别
我查了一下 auto好像是用来声明自动变量,那它的本质如果不是vector<B>::iterator
那是什么呢?
(看不明白代码没事,是因为我简化了,我原来的代码实现功能有点复杂了就没抄下来)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-24 21:08:07 | 显示全部楼层
人造人 发表于 2019-6-24 20:57
第6行定义了变量ptr_c,并且ptr_c为VC.begin(),因为VC为空,所以ptr_c == VC.begin() == VC.end() ...

不理解了,VC不是传进来一个吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-24 21:16:06 | 显示全部楼层    本楼为最佳答案   
newu 发表于 2019-6-24 21:08
不理解了,VC不是传进来一个吗?


报错
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

class A
{
protected:
        int a = 5;
};

class B :public A
{
public:
        void SetA(int i) { a = i; }
        int GetA() { return a; }
};

class C
{
protected:
        int c = 0;
        vector<A> a;
public:
        void SetC(int i){        c = i;}
        int GetC()        {        return c;}
};

void insert(vector<B>& VB, vector<C>& VC)
{
        C c;
        c.SetC(5);
        vector<B>::iterator ptr_b = VB.begin();
        vector<C>::iterator ptr_c = VC.begin();
        for (ptr_b; ptr_b < VB.end(); ptr_b++)
        {
                for (ptr_c; ptr_c < VC.end(); ptr_c++)  //这里刚刚出现错误,手误打错了
                        if ((*ptr_b).GetA() == (*ptr_c).GetC())
                                break;
                if (ptr_c == VC.end())
                {
                        c.SetC((*ptr_b).GetA());
                        VC.push_back(c);
                }
        }
}

int main()
{
        B b;
        vector<B> vb;
        vector<C> vc;
        for (int i = 0; i < 5; i++)
        {
                b.SetA(i);
                vb.push_back(b);
        }

        insert(vb, vc);
        return 0;
}



不报错
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

class A
{
protected:
        int a = 5;
};

class B :public A
{
public:
        void SetA(int i) { a = i; }
        int GetA() { return a; }
};

class C
{
protected:
        int c = 0;
        vector<A> a;
public:
        void SetC(int i){        c = i;}
        int GetC()        {        return c;}
};

void insert(vector<B>& VB, vector<C>& VC)
{
        C c;
        c.SetC(5);
        vector<B>::iterator ptr_b = VB.begin();
        for (ptr_b; ptr_b < VB.end(); ptr_b++)
        {
                vector<C>::iterator ptr_c = VC.begin();
                for (ptr_c; ptr_c < VC.end(); ptr_c++)  //这里刚刚出现错误,手误打错了
                        if ((*ptr_b).GetA() == (*ptr_c).GetC())
                                break;
                if (ptr_c == VC.end())
                {
                        c.SetC((*ptr_b).GetA());
                        VC.push_back(c);
                }
        }
}

int main()
{
        B b;
        vector<B> vb;
        vector<C> vc;
        for (int i = 0; i < 5; i++)
        {
                b.SetA(i);
                vb.push_back(b);
        }

        insert(vb, vc);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-24 21:17:21 | 显示全部楼层
void insert(vector<B>& VB, vector<C>& VC)
{
        C c;
        c.SetC(5);
        vector<B>::iterator ptr_b = VB.begin();
        vector<C>::iterator ptr_c = VC.begin();
        for (ptr_b; ptr_b < VB.end(); ptr_b++)
        {
                for (ptr_c; ptr_c < VC.end(); ptr_c++)  //这里刚刚出现错误,手误打错了
                        if ((*ptr_b).GetA() == (*ptr_c).GetC())
                                break;
                if (ptr_c == VC.end())
                {
                        c.SetC((*ptr_b).GetA());
                        VC.push_back(c);
                }
        }
}

void insert(vector<B>& VB, vector<C>& VC)
{
        C c;
        c.SetC(5);
        vector<B>::iterator ptr_b = VB.begin();
        for (ptr_b; ptr_b < VB.end(); ptr_b++)
        {
                vector<C>::iterator ptr_c = VC.begin();
                for (ptr_c; ptr_c < VC.end(); ptr_c++)  //这里刚刚出现错误,手误打错了
                        if ((*ptr_b).GetA() == (*ptr_c).GetC())
                                break;
                if (ptr_c == VC.end())
                {
                        c.SetC((*ptr_b).GetA());
                        VC.push_back(c);
                }
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-24 21:18:26 | 显示全部楼层
人造人 发表于 2019-6-24 20:57
第6行定义了变量ptr_c,并且ptr_c为VC.begin(),因为VC为空,所以ptr_c == VC.begin() == VC.end() ...

如果VC为空就36行 for (ptr_c; ptr_c < VC.end(); ptr_c++)这个循环就跳出了呀,后面我就要往VC后面插入B的元素了,这样逻辑应该没问题吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-24 21:18:33 | 显示全部楼层

看的我眼花,不看了大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-24 21:28:12 | 显示全部楼层
本帖最后由 VOGHOST 于 2019-6-24 21:37 编辑


多谢大佬
看懂了,原来是里面那重循环的ptr_c在外层循环第二次循后环没有指向VC.begin();
真的没注意看
这个错误我以前也经常犯,没想到又栽在这上面
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-24 21:40:20 | 显示全部楼层
VOGHOST 发表于 2019-6-24 21:28
多谢大佬
看懂了,原来是里面那重循环的ptr_c在外层循环第二次循后环没有指向VC.begin();
真的没注意 ...

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 21:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表