Sign in to follow this  

Can't get rid of silly warning in abstract class

This topic is 3376 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I get the error 'Warning 1 Field 'MazeRunner.Number<Child>.Zero' is never assigned to, and will always have its default value '
    abstract class Number<Child> : Ordered<Child> where Child : Number<Child>
    {
        static Child Zero;   //ERROR IS HERE!
        public abstract Child Get();

        public bool IsPositive()
        {
            return (Get() >= Zero);
        }
    }

    class Int : Number<Int>
    {
        static Int Zero = new Int(0);
        int value;
        ..
    }

The field 'Zero' only has a value in Child classes. [Edited by - steenreem on September 15, 2008 7:13:45 AM]

Share this post


Link to post
Share on other sites
So.. I should make zero a method then?

Mr. Monkey, I was experimenting with templates and that's when I made the number class. The whole thing is here below. The templates get kinda crazy! =D


abstract class Equatable<Child> where Child : Equatable<Child>
{
public abstract bool Equals(Child c);

public static bool operator ==(Equatable<Child> f, Equatable<Child> s)
{
return (s.Equals((Child)f));
}
public static bool operator !=(Equatable<Child> f, Equatable<Child> s)
{
return !(s.Equals((Child)f));
}

public override int GetHashCode()
{
return base.GetHashCode();
}

public override bool Equals(System.Object s)
{
return base.Equals(s);
}
}

abstract class Ordered<Child> : Equatable<Child> where Child : Ordered<Child>
{
abstract public bool Smaller(Child s);
public static bool operator <(Ordered<Child> f, Ordered<Child> s)
{
return (f.Smaller((Child)s));
}
public static bool operator >(Ordered<Child> f, Ordered<Child> s)
{
return (s.Smaller((Child)f));
}
public static bool operator >=(Ordered<Child> f, Ordered<Child> s)
{
return (s.Smaller((Child)f) || s.Equals((Child)f));
}
public static bool operator <=(Ordered<Child> f, Ordered<Child> s)
{
return (f.Smaller((Child)s) || s.Equals((Child)f));
}

public override bool Equals(Child s)
{
return ((this < s) && (this > s));
}

public override int GetHashCode()
{
return base.GetHashCode();
}
}

abstract class Number<Child> : Ordered<Child> where Child : Number<Child>
{
static Child Zero;
public abstract Child Get();

public bool IsPositive()
{
return (Get() >= Zero);
}

abstract public Child Divide(Child f);

public static Child operator /(Number<Child> f, Child s)
{
return f.Divide(s);
}

}

class Int : Number<Int>
{
static Int Zero = new Int(0);
int value;

public Int Random(Int i)
{
Random r = new Random();
return r.Next(this, i);
}

public override Int Get(){return this;}

public override bool Equals(Int s)
{
return value == s;
}

public override bool Smaller(Int s)
{
return value < s;
}

public static implicit operator int(Int I)
{
return I.value;
}

public static implicit operator Int(int i)
{
return new Int(i);
}

public Int(int boot)
{
value = boot;
}

public override Int Divide(Int s)
{
return new Int(this/s);
}

public Int Half()
{
return (this / 2);
}

}

Share this post


Link to post
Share on other sites

This topic is 3376 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this