0

I'm trying to add two object that they are in the same class.

In the private section of the class I have two int variables

class One {

private:
int num1, num2;
public:

    One operator+=(const One&); // - a member operator that adds another One object - to the current object and returns a copy of the current object
    friend bool operator==(const One&, const One&); // - a friend operator that compares two One class objects for equality
};

 One operator+(const One&, const One&);// - a non-friend helper operator that adds One objects without changing their values and returns a copy of the resulting One 

I'm not sure I have a problem on the opeartor+ I guess

One operator+(const One &a, const One &b){

One c,d,r;

c = a;
d = b;

r += b;
r += a;

return r;
}

I think the above code is wrong, but I tried to use like b.num1 and I get compile error

error: 'int One::num1' is private error: within this context

and I can't use b->num1 as well because the above function is not in the member function section.

error: base operand of '->' has non-pointer type 'const One'

This is how it calls in main

Result = LeftObject + RightObject;

8
  • Whe the operator+ is wrong ? BTW: you forget to initialize the members. Commented Jun 27, 2012 at 15:26
  • You cant do b.num1, num1 is a private member. If you need to access it you need to have a getter function for that. b-> is invalid as you have not created a pointer type. Do you intended to add the private integers? Commented Jun 27, 2012 at 15:27
  • @another.anon.coward how can I get that? I just want to find a way to do a copy of two objects and then return them in object's name r Commented Jun 27, 2012 at 15:28
  • I am not sorry, I dont quite understand. What should operator + add? To get private create a public function which returns the private member maybe - public: inline int getInt1(){return int1;} Commented Jun 27, 2012 at 15:30
  • @another.anon.coward in the operator+ it suppose to add two objects together and assign it to object call r then return that to the main function in main the calling of this operator is like this Result = Left + right; Commented Jun 27, 2012 at 15:32

2 Answers 2

6

If you have already implemented this member function:

One One::operator+=(const One&);

Then you may implement the non-member addition operator thus:

One operator+(const One& lhs, const One& rhs) {
  One result = lhs;
  result += rhs;
  return result;
}

This can be simplified somewhat into the following:

One operator+(One lhs, const One& rhs) {
  return lhs += rhs;
}

This pattern (which you can adapt for all operator/operator-assignment pairs) declares the operator-assignment version as a member -- it can access the private members. It declares the operator version as a non-friend non-member -- this allows type promotion on either side of the operator.

Aside: The += method should return a reference to *this, not a copy. So its declaration should be: One& operator+(const One&).


EDIT: A working sample program follows.

#include <iostream>
class One {
private:
  int num1, num2;

public:
  One(int num1, int num2) : num1(num1), num2(num2) {}
  One& operator += (const One&);
  friend bool operator==(const One&, const One&);
  friend std::ostream& operator<<(std::ostream&, const One&);
};

std::ostream&
operator<<(std::ostream& os, const One& rhs) {
  return os << "(" << rhs.num1 << "@" << rhs.num2 << ")";
}

One& One::operator+=(const One& rhs) {
  num1 += rhs.num1;
  num2 += rhs.num2;
  return *this;
}

One operator+(One lhs, const One &rhs)
{
  return lhs+=rhs;
}

int main () {
  One x(1,2), z(3,4);
  std::cout << x << " + " << z << " => " << (x+z) << "\n";
}
Sign up to request clarification or add additional context in comments.

9 Comments

I don't think you should modify the lhs
I don't. In both instances I modify a local copy of the lhs. In the first instance, I make the copy explicitly. In the second instance, I ask the compiler to make a copy (because lhs is passed by value, not passed by reference.)
It works, but my result still wrong, probably my another function is not complete.
The operator I list depends upon a working copy constructor and a working operator +=. Yes, if either of those are incorrect, the result will be incorrect.
@Ali - I've included a sample program that might help you.
|
1

I can't see why the operator+ is wrong:

#include <stdio.h>

class One {
    public:
        One(int n1, int n2): num1(n1),num2(n2) {}

    private:
        int num1, num2;
    public:
        One operator+=(const One& o) {
            num1 += o.num1;
            num2 += o.num2;
            return *this;
        }
        friend bool operator==(const One&, const One&); // - a friend operator that compares two One class objects for equality
        void print() {
            printf("%d,%d\n", num1, num2);
        }
};

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

int main() {
    One a(1,2),b(3,4);
    One r = a + b;
    r.print();
}

2 Comments

It's usual to copy one argument and then add the other to it (i.e. One r=a; r+=b;). That will work even if the type doesn't have an additive identity.
#include <cstdio> maybe? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.