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

Overloading operator+ to add class objects stored in a vector

本文关键字:向量 对象 存储 运算符 添加 重载      更新时间:2023-10-16

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

我通过以下方式超载了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);
}

我想遍历文件的每一行,并添加所有xArgyArg,并在最后显示总和。在我看到的所有教程中,添加是通过创建一个单独的类对象并为其设置值来完成的,然后创建"Sum"对象并将对象加在一起。

我的问题是我不知道输入文件中将存在多少力,那么我将如何添加xArgyArg来创建Force Sum对象?

这是我的一些课程:

Force::Force() {};
Force::Force(char fType, float xAr, float yAr)
{
    forceType = fType;
    xArg = xAr;
    yArg = yAr;
}

以下是我的一些main功能:

void 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);    //stores the class objects in a vector force
    }
    for (int i = 0; i < force.size(); ++i) {
        force[i].printforce();    //this just prints the vector
    }

我对这一切真的很陌生,所以希望这能解释清楚,我可以得到一些帮助。

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

首先

,您的operator+实现不正确,用于表达式:

x = a + b;

this你的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
Force sum('c',0,0);
while (file >> type >> x >> y) {
    Force f(type, x, y);
    sum = sum + f; // operator overloading is active here 
}

确保为">="编写另一个重载

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

  1. 过载operator+= .然后在循环中使用它来更新对象。
  2. 过载operator+ .然后在循环中使用它来更新对象。

如果你使用 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+=方面实现。

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);
}

尝试这样的事情:

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;
}