-11

Say i have a class

class var1
{
   int a;
   int b;

}
var1 var1::operator+(var1 passed)
   {   
       var1 Result;

       Result.a = a + passed.a;
       Result.b = b + passed.b;

       return Result;
   }

Now i can do simple overloading in Main. but how do i this overloading in another class.

class var2
{
   public:
         var1 res;
         var1 res2;
}
void var2::add()
{
   var1 temp;
   temp = res + res2;
}
Win Coder
  • 6,628
  • 11
  • 54
  • 81
  • 1
    Is there a "void add();" somewhere in your var2 class? there's not in the sample above. The code for the operator overload is definitely being included and such right? What's the error, we need more info! By the way it is not special that it is being overloaded within another class. – Alec Teal Sep 20 '13 at 19:24
  • I'm not sure on what you're asking. Could you please clarify. – Rahul Shardha Sep 20 '13 at 19:25
  • ahh i quickly typed the sample just consider my scenario – Win Coder Sep 20 '13 at 19:25
  • @AlecTeal, the same question for the `operator+` function (since it is defined as `var1::operator+`. – Shoe Sep 20 '13 at 19:25
  • Its simple var1 operator overloading to be used in var2 class – Win Coder Sep 20 '13 at 19:25
  • come on guys is it so difficult problem ? – Win Coder Sep 20 '13 at 19:27
  • 7
    @WinCoder, you are not explaining the problem at all. Your code is all bugged out and you refuse to fix it. Your question have grammar errors all over the place and there's no sense in the question you are making. How do you expect us to deal with this? – Shoe Sep 20 '13 at 19:28
  • 1
    what's you base value for a in the first function? isn't a and b undefined in that function? – Rahul Shardha Sep 20 '13 at 19:29
  • ahhh i was hoping for a quick answer and hoping that someone might get the idea so that i don't have the post the whole code Alas it seems there is no easy way out of this mess. – Win Coder Sep 20 '13 at 19:31
  • 1
    My downvote is for lack of willingness to actually express the problem. Although we can guess at what you're trying to do, you seem to know what your problem is and were trying to shortcut your information. We can't give you the best answer if you don't give us the best information you have. – Will Custode Sep 20 '13 at 19:43

1 Answers1

1

To provide potentially more information about what you may be trying to do, see the following example:

class var1
{
    int a, b;

public:
    var1 operator+( const var1& rhs )
    {
        var1 output;

        output.a = this->a + rhs.a;
        output.b = this->b + rhs.b;

        return output;
    }
};

class var2
{
    var1 res1, res2;
public:
    var2 operator+( const var2& rhs )
    {
        var2 output;

        output.res1 = this->res1 + rhs.res1;
        output.res2 = this->res2 + rhs.res2;

        return output;
    }
};

This is not overloading, but based on your code it seems like this might be what you are trying to accomplish. If you add more detail to your question I can better tailor this answer to your needs.

To explain this example, because the addition operator is defined in var1 it can be used to sum the respective fields of var2. By defining your own addition operator on var2 you can use the operator your defined in var1 to perform a logical summation of parts and return the result. This is not overloading or overriding anything, because the two classes have no relationship to each other.

For more information on operator overloading in C++, read this on SO.

Community
  • 1
  • 1
Will Custode
  • 4,576
  • 3
  • 26
  • 51
  • My Problem was that i had only overloaded operator for adding single variables and instead i was using them to add variables from arrays. – Win Coder Sep 20 '13 at 20:08