报错#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;
}
|