C
C#2y ago
phattanuki

✅ Inherited a class but the compiler does not accept it?

I've created two classes, Head and Knot. Head inherits Knot with another caveat of having an additional method on it, yet the compiler gives the error of "There is no argument given that corresponds to the required parameter '_coordinate' of 'Knot.Knot(Point)'". Am I misunderstanding something here?
//This is only to make the code more readable
public class Knot{
public Point Coordinate{get;set;}

public Knot(Point _coordinate){
Coordinate = _coordinate;
}

//This method checks if the current knot is touching the previous knot in the rope in any way in a 3X3 grid
//If true, the current knot is not supposed to move
public bool Touch(Knot head){
for(int scanX = -1; scanX < 2; scanX++){
for(int scanY = -1; scanY < 2; scanY++){
Point check = new Point(this.Coordinate.X + scanX, this.Coordinate.Y + scanY);
if(check == head.Coordinate) return true;
}
}
return false;
}
}

public class Head : Knot{

void moveHead(string direction){
//move head one step at a time
if(direction is "U") Coordinate.X--;
if(direction is "D") Coordinate.X++;
if(direction is "L") Coordinate.Y--;
if(direction is "R") Coordinate.Y++;
}
}
//This is only to make the code more readable
public class Knot{
public Point Coordinate{get;set;}

public Knot(Point _coordinate){
Coordinate = _coordinate;
}

//This method checks if the current knot is touching the previous knot in the rope in any way in a 3X3 grid
//If true, the current knot is not supposed to move
public bool Touch(Knot head){
for(int scanX = -1; scanX < 2; scanX++){
for(int scanY = -1; scanY < 2; scanY++){
Point check = new Point(this.Coordinate.X + scanX, this.Coordinate.Y + scanY);
if(check == head.Coordinate) return true;
}
}
return false;
}
}

public class Head : Knot{

void moveHead(string direction){
//move head one step at a time
if(direction is "U") Coordinate.X--;
if(direction is "D") Coordinate.X++;
if(direction is "L") Coordinate.Y--;
if(direction is "R") Coordinate.Y++;
}
}
13 Replies
Jayy
Jayy2y ago
How many constructors does knot have
phattanuki
phattanuki2y ago
one the constructor is not also inherited? does head need it's own constructor?
Thinker
Thinker2y ago
yes It needs a constructor which calls : base(Point)
phattanuki
phattanuki2y ago
huh...that's interesting...so even though I'm fully inhering from Knot... I still need my own constructor
Jayy
Jayy2y ago
Yes
phattanuki
phattanuki2y ago
I've learned
Jayy
Jayy2y ago
I mean ya, how would it work otherwise
phattanuki
phattanuki2y ago
it inherits Knots constructor? since it's the same object? and if I need something else, I can override it? but that's just my poor brain
Thinker
Thinker2y ago
constructors are not inherited
phattanuki
phattanuki2y ago
roger
Jayy
Jayy2y ago
You are saying "the only way i can build a knot is with this constructor, then inheriting the class and not using that constructor. So there's a world where someone calls new Head() Which breaks the assertion that Knot must be created with that constructor If you added an empty constructor to Knot it would work
Hugh
Hugh2y ago
In my mind, as, I suspect, in @phattanuki's, I would have imagined that if Head doesn't have a constructor defined, it would default to:
public Head(Point _coordinate) : base(_coordinate) {}
public Head(Point _coordinate) : base(_coordinate) {}
(I get that it doesn't, but before I realised that, I felt that this was a reasonable assumption)
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.