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 trialChristopher Aberly
1,241 PointsI keep getting an error when defining my class asking if I'm defining it as a subclass and if I'm doing that b4 or after
I get the following error: Bummer: Did you define your Square class as an inner class of the Polygon class? Be sure to define the Square class before or after the definition for the Polygon class.
But per the previous example I'm defining the class outside of the Polygon class's {}.
class Polygon {code} class Square : Polygon {code}
I keep going back to the examples and I can't figure out why I get this error.
Additionally, the task requires a "single" parameter for Square that is initialized by the base: call method. However, the parameter for the "Square" is length of the sides and the parameter for "Polygon" is number of sides. These two things are not related mathematically. So, I'm confused. If the number of sides is supposed to be initialized in the base method of Polygon class through the Square class shouldn't there be a parameter for the number of sides?
namespace Treehouse.CodeChallenges
{
class Polygon
{
public readonly int NumSides;
public Polygon(int numSides)
{
numSides = 4;
NumSides = numSides;
}
}
class Square : Polygon
{
public readonly int SideLength;
public Square(int sideLength) : base(NumSides)
{
}
}
}
2 Answers
Steven Parker
231,269 PointsYou should not make any changes to the provided "Polygon" class. Apparently modifying it confused the system and caused that bizarre message.
Also, when they say "Use base to initialize NumSides to 4.", that's done like this:
public Square(int sideLength) : base(4) // this puts 4 into NumSides
And you still need to initialize "SideLength" in the constructor.
Christopher Aberly
1,241 Pointsgot it. Thanks Steven! I only modified the Polygon class trying different things to see what would work. Thanks for the tip on the base(4)! Didn't realize that.