Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
class space
{
    int x, y, z;
    public space(int a, int b, int c)
    {
        x = a;
        y = b;
        z = c;
    }
    public void display()
    {
        Console.WriteLine("" + x);
        Console.WriteLine("" + y);
        Console.WriteLine("" + z);
        Console.WriteLine();
    }
    public static space operator -(space s)
    {
        //return new space(-s.x, -s.y, -s.z); 
        s.x = -s.x;
        s.y = -s.y;
        s.z = -s.z;
        return s;
    }
}
static void Main()
{
    space s = new space(10,-50,32);
    Console.Write("s:");
    s.display();
    -s;
    Console.Write("s:");
    s.display();
    Console.ReadLine();
}
Posted
Updated 11-Nov-11 0:31am
v2
Comments
Slacker007 11-Nov-11 6:32am    
Your question is incomplete. Please explain your question/situation in more detail please. Thanks.

The problme is as the error says:
C#
-s;


Only assignment, call, increment, decrement, and new object expressions can be used as a statement

"-s" is none of those - you would have to assign the value somewhere in order for it to be acceptable.

C#
space x = -s;
Console.Write("x:");
x.display();


BTW: It is very bad practice to affect the current item in a negation operator: it is not clear that that would occur. Think about it: does it happen when you do this:
C#
int i = 17;
int j = i + -i;
No, j is assigned zero. If the negation operator worked your way, then it would be assigned -34, probably...
Use the commented out version to return a new instance instead.
 
Share this answer
 
change your main function like this:

static void Main(string[] args)
{
   space s = new space(10, -50, 32);
   Console.Write("s:");
   s.display();
   s = -s;
   Console.Write("s:");
   s.display();
   Console.ReadLine();
}
 
Share this answer
 
What you trying to do with over loading a - operator....

you use a minus operator to over load and use that for the task looks like to decrement operator perform..

you need to use a minus operator for performing only subtrac activity....
and if you want to perform decrement then use decrement operator to over load...

like this,,,,

C#
class space
{
    int x, y, z;
    public space(int a, int b, int c)
    {
        x = a;
        y = b;
        z = c;
    }
    public void display()
    {
        Console.WriteLine("" + x);
        Console.WriteLine("" + y);
        Console.WriteLine("" + z);
        Console.WriteLine();
    }
    public static space operator --(space s) // use decrement operator to over load....
    {
        //return new space(-s.x, -s.y, -s.z); 
        s.x = -s.x;
        s.y = -s.y;
        s.z = -s.z;
        return s;
    }
}
class Program
{
    static void Main(string[] args)
    {
        space s = new space(10, -50, 32);
        Console.Write("s:");
        s.display();
        --s;  // decrement operator used like this....
        // or if you want to over load your minus operator then use like this...
       // s = - s;
        Console.Write("s:");
        s.display();
        Console.ReadLine();
    }
}


see this for operator over loading...
http://www.csharp-station.com/Tutorials/Lesson18.aspx[^]
 
Share this answer
 
v3
You are going to have to put s = -s; (I take it this is what you mean). You cannot put -s; on it's own in the main function.
 
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