• Post Reply Bookmark Topic Watch Topic
  • New Topic

error: invalid method declaration; return type required?  RSS feed

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm trying to write a class Line that forms a line from two points. I have a separate Line main and Point class.
I'm getting errors saying that I need to have a return type for my LineAB method? I'm not sure why, as I mean for it to be a constructor. Do I have a logic error?
I have my other try in comments.
Any help is appreciated, thanks!


 
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The name of the constructor should match the name of your class. You've called the class Line, but the constructor is LineAB.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Riaan's got your initial problem. You've done one constructor correctly, so it's a bit strange that you thought the second should be different .

Once you've fixed that, it still won't do what you want though. These lines:
define local variables pA and pB, which you assign to. These hide the instance variables pA and pB, which will still be null.

Additionally, why do you have different instance variables p1/p2 and pA/pB? It looks like you only need one pair, and both constructors should initialise that pair. Otherwise you're going to get some very confusing behaviour.
 
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are trying to create an one more constructor which should be "public Line(int x1, int y1, int x2, int y2)", if a class contains one more constructors, then all of that constructor should be bearing the same name as of the class name. So your class "Line" will contain constructor only with the name "Line"

Example -
class Line


and so your line of code



is just an another method inside class Line and so it requires a return type like void or int.
Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!