-1

I got a class called "player" which I have to use in my project, it looks like this:

template<typename F>
class player {
public:
  int play(const F &field);
};

Now I want to use the function "play" in my Game and I tried calling it like this:

player<Game> player2();
player2.play(this);

I got the following error:

error: request for member 'play' in 'player2', 
which is of non-class type 'player<Game>()'

I have no idea why this is, I did similar things with other classes and it worked just fine ... Could someone please help me? (I'm using eclipse and mingw gcc)

Edit:

Ok everything works now. Thank you very much.

Ben
  • 109
  • 1
  • 11

2 Answers2

2

This code:

player<Game> player2();

Is a declaration of function player2 that takes no arguments and returns player<Game> as value. change it to:

player<Game> player2;

Note, it worked for you before because you used constructors with parameters something like this:

Foobar foo( 123 );

In this case it cannot be interpreted as function declaration and works as intended.

EDIT: method

int play(const F &field);

expects a const reference ot type F, what you are trying to pass is a pointer. You probably should dereference this:

player2.play( *this );

Slava
  • 43,454
  • 1
  • 47
  • 90
  • Furthermore he will have to dereference `this` for the types to match up. But one error message at a time... – bstamour Nov 06 '13 at 21:03
  • Ok thanks guys. I changed ist to: player player2; player2.play(this); Now I get the following Error: "undefined reference to `player::play(Game const*)'" – Ben Nov 06 '13 at 21:09
  • @Ben `play` expects to get const reference you are trying to pass const pointer. I assume you need to put `*this` as parameter. – Slava Nov 06 '13 at 21:13
  • @Slava I made a little mistake before, that was the wrong error message. When using "*this" I actually got the same error as before when using "this": " undefined reference to `player::play(Game const&)' " I edited the code in my original Post – Ben Nov 06 '13 at 21:29
  • @Ben you have to implement function. As it is a template function it's implementation should be visible where you use it or you have to explicitly instantiate it. – Slava Nov 06 '13 at 23:37
  • Thx. Of course I implemented the fuction but maybe i did it wrong? I have a player.h and a player.cpp file. The code is in my first post, I edited it. I also tried to make an instance like this: player* player2 = new player(); but I still got Errors ("undefined reference to `player::play(Game const&)'") – Ben Nov 07 '13 at 00:23
  • @Ben you should read comments, not just half of it. Try to move implementation of play into header. – Slava Nov 07 '13 at 00:27
  • @Slava Oh I understood that wrong. Yeah, that worked, i didn't know you cant implement the function in a cpp-File. Now everything works fine. Thank you very much :-) – Ben Nov 07 '13 at 08:53
0

The parameter to play needs to be of type Game not of type player.

Game myGame;
player<Game> player2;
player2.play(myGame);

Should work.

  • I actually call the fuction within the class "Game", but if I use "this" as an argument I get "undefined reference to `player::play(Game const*)'" – Ben Nov 06 '13 at 21:13
  • ok, you should use player2.play(*this); since "this" is a pointer to a Game object and not a reference to a game object, like "*this" is. – Mark Hendrickson Nov 06 '13 at 23:36