如何重载访问类属性的结构运算符?

时间:2019-12-12 20:44:43

标签: c++

我想在结构中重载+运算符以实现串联运算符。为此,我需要访问插入该结构的一些类成员(matrixroute),如下面的代码所示。

MLP.h

class MLP{
     private:
          std::vector<int> route;
          std::vector<std::vector<tCost>> cost;
          double **matrix;

     ...
}

Structures.h

typedef struct subsequenceCost{
     int w;
     double t;
     double c;

     int first,
         last;

     subsequenceCost operator+(const subsequenceCost& other) const{
          return subsequenceCost{
               w + other.w,
               t + matrix[route[last]][route[other.first]] + other.t,
               c + other.w*(t + matrix[route[last]][route[other.first]]) + other.c,
               first,
               other.last,
          };
     }
}tCost;

我已经尝试过extern我将使用的MLP对象,并将subsequenceCost结构声明为friend,以便可以访问其私有成员,但没有用因为MLP是tCost的组成。关于如何做的任何想法?

0 个答案:

没有答案