STL的VECTOR不能这样用吗?

来源:百度知道 编辑:UC知道 时间:2024/05/22 01:08:30
#include <iostream>
#include <vector>
using namespace std;

struct Position
{
Position(int i=0,int j=0,int typ=-1):u(i-1),v(j-1),type(typ){}
int u,v;
int type;
Position& operator = (Position& pos1)
{
u = pos1.u;
v = pos1.v;
type = pos1.type;
return (*this);
}
};

struct Node
{
public:
Node(Position p=NULL,
Node* parent=NULL,
int g=0,
int h=0,
Node* nex=NULL):pos(p),parent(parent),next(nex),G(g),H(h),F(g+h){}
Position pos;
Node* parent;
Node* next;
int G,H,F;
};

void main()
{
Position tempPos(1,1,1);
Node* temp = new Node(tempPos,NULL,1,1);
vector<Position> vec;
vec.push_back(temp->pos);
delete temp;
}
帮忙找下错误原因。
怎样改正呢

Position& operator = (Position& pos1)
{
u = pos1.u;
v = pos1.v;
type = pos1.type;
return (*this);
}
屏蔽掉这段就可以了,
可能默认的处理方法被改了。

那个重载=的代码和默认的实现是一样的,只是参数少了一个const的修饰,其效率还不如默认的实现。我估计你的代码错误是编译不过,需要加上const,或者另外写一个参数为const Position&的赋值操作符。