How do I define operator = for a struct in c ++?

1

I have the following structure:

struct PATH {

  vector<ARCO_TEMPO> rota;
  set<int> rotaAux;
  float COST;
  int nArcosVoyage;
  vector<bool> VisitedNodes;
  vector<vector<bool>> VisitedVertices;
  int head_no;
  int head_time;
  int tail_no;
  int tail_time;
  float reducedCost;
  float duracao;
  int auxNumAircrafts;

};

How do I create operator = for this structure? I thought I could do it like this:

PATH& PATH::operator=(const PATH& p1) {

  COST = p1.COST - cFixo[aa]*p1.auxNumAircrafts;
  reducedCost = p1.reducedCost - cFixo[aa]*p1.auxNumAircrafts;
  auxNumAircrafts = p1.auxNumAircrafts;

  nArcosVoyage = p1.nArcosVoyage;
  head_no = p1.head_no;
  head_time = p1.head_time;
  tail_no = p1.tail_no;
  tail_time = p1.tail_time;
  duracao = p1.duracao;

  for (int i = 0; i < p1.rota.size(); i++) {
    rota[i].a.i = p1.rota[i].a.i; 
    rota[i].a.j = p1.rota[i].a.j; 
    rota[i].slotTimeU = p1.rota[i].slotTimeU; 
    rota[i].slotTimeV = p1.rota[i].slotTimeV; 
  }

  for (int i = 0; i < n; i++) {
    VisitedNodes[i] = p1.VisitedNodes[i];
  }

  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= max_slot; j++) {
        VisitedVertices[i][j] = p1.VisitedVertices[i][j];
    }
  }

  rotaAux = p1.rotaAux;

  return *this;

};

However, the following error appears:

  

error: definition of implicitly-declared 'PATH & PATH :: operator = (const   PATH &) 'PATH & PATH :: operator = (const PATH & p1);

How can I fix this?

    
asked by anonymous 03.04.2018 / 22:29

2 answers

3

You have to declare the method in the class or struct if you want to implement it. Include

PATH& operator=(const PATH& p1);

within the struct statement, along with member variables.

    
03.04.2018 / 22:47
2

If you just want to copy the values without doing anything else, and assuming c ++ 11 , you can simply do

PATH &operator=(const PATH &) = default;

within the struct, without having to define a body. No , you you must declare and set the copy builder manually, such as in the @epx response .

    
04.04.2018 / 05:37