0

I'm trying to make a queue of an interfaced type Player because I don't know what type of player will be in the queue, ie human, AI etc so I have an interface for what different players can do, ie makemove etc.

Queue<Player> players = new Queue<Player>();

However, queue cannot be instantiated because Player is an interface. How do I create a queue of an interfaced type?

6
  • but there are different types of Player implementation? How can I store them all in one queue? Commented Nov 8, 2014 at 16:28
  • 1
    @hagubear no, that's incorrect. Commented Nov 8, 2014 at 16:28
  • 1
    What is the exact and complete error message you get from the compiler? Reading it helps. Hint: Queue is an interface. You should choose what kind of Queue you want. Commented Nov 8, 2014 at 16:28
  • @JBNizet "Cannot instantiate the type Queue<Player>" Commented Nov 8, 2014 at 16:29
  • 2
    @hagu what you proposed in your comment and what appears in the answer are completley different. Commented Nov 8, 2014 at 16:36

1 Answer 1

2

You can have a variable of type Queue<Player>, but Queue itself is just an interface. You need to instantiate a concrete implementation of Queue, such as LinkedList.

e.g.

Queue<Player> players = new LinkedList<Player>();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.