Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
sir
I hae written code for operator overloading but it is not working.
here's the program :
---------------------------------
using System;
class space
{
    int x, y, z;
    public space(int a, int b, int c)
    {
        x = a;
        y = b;
        z = c;
    }
    public void display()
    {
        Console.Write("" + x);
        Console.Write("" + y);
        Console.Write("" + z);
    }
    public static space operator -(space s)
    {
        s.x = -s.x;
        s.y = -s.y;
        s.z = -s.z;
    }
}
 class TestMain
 {
     public static void Main()
     {
         space s = new space(10, -20, -40);
         Console.Write("S:");
         -s.display();
         Console.Write("S:");
         Console.Write("S:");
         -s.display();
     }

 }
it shows the following errors:
Error	1	'space.operator -(space)': not all code paths return a value	C:\Users\LENOVO PC\Documents\Visual Studio 2010\Projects\operator_overload\operator_overload\Program.cs	17	25	operator_overload
Error	2	Only assignment, call, increment, decrement, and new object expressions can be used as a statement	C:\Users\LENOVO PC\Documents\Visual Studio 2010\Projects\operator_overload\operator_overload\Program.cs	30	10	operator_overload
Error	3	Operator '-' cannot be applied to operand of type 'void'	C:\Users\LENOVO PC\Documents\Visual Studio 2010\Projects\operator_overload\operator_overload\Program.cs	33	10	operator_overload
Error	4	Only assignment, call, increment, decrement, and new object expressions can be used as a statement	C:\Users\LENOVO PC\Documents\Visual Studio 2010\Projects\operator_overload\operator_overload\Program.cs	33	10	operator_overload

What I have tried:

sir  i have tried 
-s.display() 
but no results..
Posted
Updated 21-Sep-16 8:33am
Comments
[no name] 21-Sep-16 14:30pm    
What do you think trying "-s.display()" would do? You have to correct the compilation errors first!
tusharkaushik 21-Sep-16 14:31pm    
Now i've modified the code :
------------------------------------
using System;
class space
{
int x, y, z;
public space(int a, int b, int c)
{
x = a;
y = b;
z = c;
}
public void display()
{
Console.Write("" + x);
Console.Write("" + y);
Console.Write("" + z);
}
public static space operator - (space s)
{
s.x = -s.x;
s.y = -s.y;
s.z = -s.z;
return -s;
}
}
class TestMain
{
public static void Main()
{
space s = new space(10, -20, 30);
Console.Write("S: \n\n");
s.display();

Console.Write("\n S:\n");
//Console.Write("S:\n");
s.display();
Console.ReadLine();
}

}












i have written the value (

NOw showing this output :
10,-20 30

1 solution

You need to return some value from the operator overloading method. so do something like "return s;"

For the other error, the compiler is trying to apply "-" on the method display. If you want to call display on "negative of s" then do something like "(-s).display();".

PS: I am not sure what you are trying to do there, so I cannot suggest better code. I just "fixed" what's broken.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900