0

I need to call and instance of the class ControlledTank to access the GetPlayer method which I will complete later. However, when I try to create an instance it's using the constructor not the class.

public class ControlledTank
{
 private float Tangle;
 public ControlledTank(Player player, int tankX, int tankY, Game game)
 {
 Tangle = 0;
 //throw new NotImplementedException();
 }
 public Player GetPlayer()
 {
 throw new NotImplementedException();
 }
}

I don't have the values to parse into the constructor either so I just get errors.

The result I get

Enigmativity
117k11 gold badges101 silver badges184 bronze badges
asked Nov 1, 2017 at 12:01
6
  • 3
    add empty constructor? make the GetPlayer static? Commented Nov 1, 2017 at 12:03
  • 1
    try create constructor with empty parameter. like this public ControlledTank() { } Commented Nov 1, 2017 at 12:03
  • 5
    "it's using the constructor not the class" - What does that even mean? What were you expecting to happen? Why? A constructor is used to construct an instance of a class. Your constructor expects parameters. You are not supplying those parameters. Hence the error. Commented Nov 1, 2017 at 12:03
  • 2
    How do you expect to create an instance of a class when you don´t have the information needed to do so? That is you do not invoke the constructor and provide the parameters. When calling a constructor - and this is what you have to do in order to create an instance - needs some parameters you have to provide those unless there is another parameterless constructor. Commented Nov 1, 2017 at 12:06
  • 2
    Learning to program by trial and error is no way to go about it. Constructors and constructor overloads is something you learn in the very beginning of almost any language (that supports it) and is considered fundamental / basic knowledge. Pick up a book or follow some tutorials but do not try to learn the basics by trial/error, that is just a recipe for frustration for you and everyone else involved. Commented Nov 1, 2017 at 12:07

4 Answers 4

6

Your constructor expects 4 parameters:

public ControlledTank(Player player, int tankX, int tankY, Game game)
{
 //...
}

You're passing it 0 parameters:

new ControlledTank()

Hence the error. So you have two options. Either pass the parameters needed by the constructor, or also add a constructor with the parameters (or lack thereof) which you need. For example:

public ControlledTank()
{
 Tangle = 0;
}

Note: A class can have multiple constructors. A class can also have a default constructor with 0 parameters (and no internal logic), but only if no other constructor is specified.

answered Nov 1, 2017 at 12:07

Comments

1

The error is right in front of you, you can't pass no parameters to constructor that must have parameters. Here is your options, you can decide what you want according to task in front of you:

Add default constructor:

public ControlledTank(){}

or make your constructor parameters with default values:

public ControlledTank(Player player = null, int tankX = 0, int tankY = 0, Game game = null){}

"it's using the constructor not the class" makes no sense. If you want to call constructor when you are accessing class by putting . operator after class name, you need to have static constructor (they are always parameterless):

static ControlledTank() {}

But it's useful, when you are want to initialize some static fields in it.

answered Nov 1, 2017 at 12:05

2 Comments

Adding a parameter-less constructor might not be a good advice. Some of the parameters passed in the already existing constructor might be mandatory (I would guess that a ControlledTank can't be created outside of a game, so creating one without passing the game to it might not be the correct way to handle this situation).
I understand that, I just want to give him an idea, what options do he have, so he can, according to his task, make his code working.
0

A constructor expects parameters to construct the class, and you're not providing parameters.

To fix this, you just need an empty default constructor:

public ControlledTank(){}
answered Nov 1, 2017 at 12:06

Comments

0

You need to write an empty constructor as well if you need to use it like this. The constructor is used to build the instance of the class:

public ControlledTank()
{
 Tangle = 0;
 player = new Player();
 tankX = 0;
 tankY = 0;
 game = new Game();
}
answered Nov 1, 2017 at 12:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.