重载operator +以添加存储在向量中的类对象

时间:2018-04-05 19:12:33

标签: c++ operator-overloading

我正在尝试添加存储在从文件写入的向量中的所有类对象。

我以下列方式重载了db.usertest.aggregate( [{"$match":{"objects":{"$elemMatch":{"type":"fruit","origin":"XYZ"}}}}, {"$project":{ "name":{"$let":{ "vars":{"object":{ "$arrayElemAt":[ {"$filter":{ "input":"$objects", "as":"o", "cond":{ "$and":[ {"$eq":["$$o.type","fruit"]}, {"$eq":["$$o.origin","XYZ"]} ] } }}, 0 ] }}, "in":"$$object.name" }} }}])

operator+

我想遍历文件的每一行并添加所有Force Force::operator+(const Force& f) { forceType = 'c'; Force result; result.xArg = this -> xArg + f.xArg; result.yArg = this -> yArg + f.yArg; return(result); } xArg并在最后显示总和。在我看过的所有教程中,添加是通过创建一个单独的类对象并为其设置值,然后创建“Sum”对象并将对象添加到一起来完成的。

我的问题是我不知道输入文件中会出现多少Forces,那么我如何添加yArgxArg来创建yArg对象?< / p>

以下是我的一些课程:

Force Sum

以下是我的Force::Force() {}; Force::Force(char fType, float xAr, float yAr) { forceType = fType; xArg = xAr; yArg = yAr; } 功能:

main

我对这一切都很陌生,所以希望这个解释好,我可以得到一些帮助。

PS,它是项目的一部分,它明确指出我应该使用运算符重载来总结力并显示总和。

4 个答案:

答案 0 :(得分:3)

首先,您的operator+实施不正确,表达式为:

x = a + b;
this的{​​{1}}将operator+指向&a,因此在其中forceType的分配在逻辑上是错误的。所以你要么应该使用const方法(并且编译器会拒绝这样的赋值),要么更好地使它成为非成员(可能是朋友)或静态的,这个错误会变得很明显:

 Force operator+( const Force &a, const Force &b ) // non member or static
 {
     forceType = 'c'; // error where forceType belongs to?
     ...
 }

另一件事可能更容易实现operator+=

Force &Force::operator+=(const Force& f) 
{
    forceType = 'c';
    xArg += f.xArg;
    yArg += f.yArg;
    return *this;
}

然后operator+是微不足道的:

Force operator+( const Force &a, const Force &b )
{
    Force r( a );
    r += b;
    return r;
}

但您的循环可以直接使用+=

sum += newobject;

PS,以避免混淆,并理解为什么forceType = 'c';在您的代码中不正确,我的很好看看:

a + b; // the same as
a.operator+( b ); // it is not correct to change forceType for a

关于我的案子:

a += b; // the same as
a.operator+=( b ); // a is expected to be changed including forceType member

答案 1 :(得分:1)

Force sum('c',0,0);

while (file >> type >> x >> y) {
    Force f(type, x, y);
    sum = sum + f; // operator overloading is active here 
}
  

确保为“ =

编写另一个重载

答案 2 :(得分:1)

有几种方法可以实现您的目标:

  1. 超载operator+=。然后在循环中使用它来更新对象。
  2. 超载operator+。然后在循环中使用它来更新对象。
  3. 如果您使用operator+=,则需要以下内容:

    Force sum; // Assuming the default constructor is good enough
    for ( auto& item : force ) {
       sum += item;
    }
    

    如果您使用operator+,则需要以下内容:

    Force sum;
    for ( auto& item : force ) {
       sum = sum + item;
    }
    

    值得注意的是:

    1. 使用operator+=效率更高,因为它涉及更少的复制。
    2. operator+可以operator+=
    3. 来实施
      Force& operator+=(Force const& rhs)
      {
         this->type = 'c'; // ???
         this->xArg += rhs.xArg;
         this->yArg += rhs.yArg;
         return *this;
      }
      
      Force operator+(Force const& rhs) const
      {
         Force ret = *this;
         return (ret += rhs);
      }
      

答案 3 :(得分:-1)

尝试这样的事情:

int main()
{
    char type;
    float x, y;
    vector<Force> force;

    ifstream file("ForceList.txt");
    assure(file, "ForceList.txt");

    while (file >> type >> x >> y) {
        Force f(type, x, y);
        force.push_back(f);
    }

    Force sum('c', 0, 0);
    for (int i = 0; i < force.size(); ++i) {
        sum = sum + force[i];
    }

    sum.printforce();
    return 0;
}
相关问题