Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHerman Vicens
12,540 PointsI don't know what's wrong here!!
No compile error but I keep getting this inner class error and regardless where I place the class, i get the same message. Any ideas?
thanks,
namespace Treehouse.CodeChallenges
{
class Polygon
{
public readonly int NumSides;
public readonly int SideLength;
public int Polygon(int numSides)
{
NumSides = numsides;
}
}
class Square : Polygon
{
public int Square(int sideLength) : base (int numSides)
{
NumSides = 4;
SideLength = sideLength;
}
}
}
2 Answers
andren
28,558 PointsThat error message is misleading. But there are a couple of issues with your code:
The
SideLength
field should be placed inside theSquare
class, not inside thePolygon
class.You have added
int
as a return type to thePolygon
constructor. Adding a return type to a constructor is not allowed. Constructors does not have return types. That is part of what separates a constructor from a normal method.You have changed
numSides
tonumsides
within thePolygon
constructor,numsides
is not the name of the parameter so that is incorrect.You have also added
int
as a return type to theSquare
constructor, which as explained above is not valid.Within the
base
call you are meant to pass an argument that will be passed directly into the constructor of the parent class. Meaning in this case that you should actually be passing the number 4 in directly, not defining it in the constructor.
Here is the fixed code:
namespace Treehouse.CodeChallenges
{
class Polygon
{
public readonly int NumSides;
public Polygon(int numSides)
{
NumSides = numSides;
}
}
class Square : Polygon
{
public readonly int SideLength;
public Square(int sideLength) : base (4)
{
SideLength = sideLength;
}
}
}
Herman Vicens
12,540 PointsThank you very much. It worked as you said. Now I get it!! Thanks,