【问题标题】:Is it possible to add two objects of same class using '+' operator是否可以使用“+”运算符添加同一类的两个对象
【发布时间】:2014-05-18 05:27:20
【问题描述】:

我的教科书说我们可以添加两个相同类的对象。 V3=V2+V1 //都属于同一类。

但是当我在 Turbo c++ 中测试时,我得到了错误:非法结构操作指向同一行,V3=V1+V2。

所以我的问题是是否可以使用 + 运算符添加同一类的两个对象,如果答案是肯定的,那么为什么我会收到错误消息?

【问题讨论】:

  • 如果类重载了+操作符,你可以这样做。
  • 你需要的是运算符重载:stackoverflow.com/q/4421706/69537
  • 为了得到你的答案,想想这个:你期望它做什么?
  • ...放弃 Turbo C++,就像在高速公路上骑马车

标签: c++ class


【解决方案1】:

您的类必须重载了+ 运算符。没有它,编译器将不知道如何“添加”给定的两个类。通过添加运算符重载函数来定义+ 运算符的工作方式。

以下是“V”类的示例:

V V::operator+(const V&  other){
    //Define how should the classes be added here

    //Example addition of private fields within V
    int field1 = this.field1 + other.field1;

    //Return the 'added' object as a new instance of class V
    return V(field1);
}

更完整的运算符重载参考可以查看here

【讨论】:

  • 您在范围解析运算符(第一行)中缺少一个“:”。
  • @gnometorule 糟糕,已修复。
【解决方案2】:

是的,当然你可以添加两个相同类的对象,但在此之前你必须做运算符重载,通过定义“+”运算符以及当你简单地在它们之间放置一个“+”运算符时对象将如何添加物体。 您不仅可以添加,还可以实现任何运算符,例如 '-' 、'*'、'/' 但首先你必须重载它们。

这里是一个运算符重载的例子

class Cents
{
 private:
 int m_nCents;

 public:
   Cents(int nCents) { m_nCents = nCents; }

// Add Cents + Cents
   friend Cents operator+(const Cents &c1, const Cents &c2);

   int GetCents() { return m_nCents; }
};

// note: this function is not a member function!
Cents operator+(const Cents &c1, const Cents &c2)
{
 // use the Cents constructor and operator+(int, int)
   return Cents(c1.m_nCents + c2.m_nCents);
}

int main()
{
  Cents cCents1(6);
  Cents cCents2(8);
  Cents cCentsSum = cCents1 + cCents2;
  std::cout << "I have " << cCentsSum .GetCents() << " cents." << std::endl;

  return 0;
}

【讨论】:

  • 重载成员operator+=要简单得多,而不是使用friend
【解决方案3】:

您不仅可以添加两种用户定义的数据类型,还可以使用operator overloading进行各种操作。

运算符重载的一般语法如下:

如果要添加两个对象如:

eg: objres=obj1+obj2;//belong to class s

操作obj1+obj2 应该返回一个相同的用户定义数据类型的对象。

所以,重载函数的返回类型应该是s

s operator+(s obj)
{
s temp;
temp.datamember=datamember+obj.datamember;//perform operation on datamembers
return temp;//return object to objres
}

这将返回一个对其执行特定操作的对象。

应该注意operator 是 C++ 中的一个关键字,如果运算符被重载,则不改变含义是一般道德规范。此外,如果一个运算符是一元的,它仍然是一元的,如果一个运算符是二元的,它仍然是二元的并且分别需要一个和两个操作数。

同样,*,&gt;,&lt;,==,/,++,-- 等运算符也可以重载。

【讨论】:

    【解决方案4】:
    #include<iostream>
    using namespace std;
    /*class count
    {private:
        int n;
        
            public:
            count()
            {
            n=0;        }
            void show()
            {cout<<n<<endl;
            }
            void operator ++()
            {count temp;
            n=n+1;
            temp.n=n;
            return temp;
            }
            void operator ++(int)
            {count temp;
            n=n+1;
            temp.n=n;
            return temp;
            }
            
    };
    int main()
    {
    count a,b;
    
        a.show(); 
      
    ++a;
    a++;
        a.show();
    
        
        
    }*/
    class add
    {
        private  :
            int a,b;
            public:
                void get()
                {cout<<"enter a";
                    cin>>a;
                //  cout<<"enter b";
                //  cin>>b;
                }
                void show()
                {
                cout<<"sum of a"<<a<<endl;  
                }
                add operator +(add x)
                {
                //  add y;
                    y.a=x.a+a;
                //  return y;
                    
                }
            
    };
    int main()
    {
        add obj1,obj2,obj3;
        obj1.get();
        obj2.get();
        obj3=obj1+obj2;
        obj3.show();
    }
    

    【讨论】:

    • 虽然这可能会回答问题,但最好对您提供的解决方案进行简要说明,这样可能会使 OP 和未来的读者受益。
    猜你喜欢
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 2015-12-14
    • 2012-08-07
    • 1970-01-01
    • 2014-09-07
    相关资源
    最近更新 更多