报错严重性 代码 说明 项目 文件 行 禁止显示状态
错误(活动)
E0551 当前范围内无法定义 type "std::vector<_Ty, _Alloc>::iterator [其中 _Ty=Person, _Alloc=std::allocator]" 191
严重性 代码 说明 项目 文件 行 禁止显示状态
错误
C2226 语法错误: 意外的“std::vector<Person,std::allocator<_Ty>>::iterator”类型 ConsoleApplication3
//请大家帮忙看看怎么改
class Person
{
public:
Person(string name, int age,int height);
~Person();
private:
string m_Name;
int m_Age;
int* m_Height;//这里有一个堆区数据
friend class MyPrint;
friend void myCopy(const vector<int*>& vSource, vector<int*>& vTarget);
friend class MyCopy;
};
Person::Person(string name, int age, int height)
{
this->m_Name = name;
this->m_Age = age;
this->m_Height = new int(height);
}
class MyCopy
{
public:
void operator()(const vector<Person>& vSource, vector<Person>& vTarget)
{
for (vector<Person>::const_iterator its = vSource.begin(), vector<Person>::iterator itt = vTarget.begin(); its != vSource.end(); its++,itt++)
//我这里的意思是
{
itt = its;//its就是原容器迭代器,itt就是目标容器迭代器,让目标容器的每一个元素=原容器
//深拷贝,否则堆区数据重复释放报错
itt->m_Height = new int (*(its->m_Height)); //its->m_Height是指针// *(its->m_Height)就是身高的值了
}
}
};
vector<Person>::const_iterator a,vector<Person>::iterator b
这样的语句肯定是错误的,类似于
int a,char b
你正确的方案,是给Persion加上copy构造,和operator= ,然后直接vTarget.insert(vTarget.begin(),vSource.begin(),vSource.end());