Trouble using function pointers as parameters.

T

Thomas Nelson

I'm trying to write a function that uses function pointers. Here's my
errors:
Board.cc:111: error: expected ',' or '...' before 'p1'
Board.cc:111: error: prototype for 'int Board::play_game(int (*)(const
Board&))' does not match any in class 'Board'
Board.h:18: error: candidate is: int Board::play_game(int (*)(const
Board&), int (*)(const Board&), bool)


Here's Board.cc:111:

int Board::play_game(int(const Board &) p1, int(const Board &) p2,
bool silent=false)

And here's Board.h:18:

int play_game(int(const Board &), int(const Board &), bool
silent);

What have I done wrong? I wanted to use a typedef to simplify this,
but Board has to be declared to use it in a typedef, and I want to use
the typedef in my Board declaration. Any help would be great.

Thanks,
Tom
 
V

Victor Bazarov

Thomas said:
I'm trying to write a function that uses function pointers. Here's my
errors:
Board.cc:111: error: expected ',' or '...' before 'p1'
Board.cc:111: error: prototype for 'int Board::play_game(int (*)(const
Board&))' does not match any in class 'Board'
Board.h:18: error: candidate is: int Board::play_game(int (*)(const
Board&), int (*)(const Board&), bool)


Here's Board.cc:111:

int Board::play_game(int(const Board &) p1, int(const Board &) p2,
bool silent=false)

And here's Board.h:18:

int play_game(int(const Board &), int(const Board &), bool
silent);

What have I done wrong? I wanted to use a typedef to simplify this,
but Board has to be declared to use it in a typedef, and I want to use
the typedef in my Board declaration. Any help would be great.

Read the FAQ 5.8. That's for the next time. For now, however, try
changing it to

/* in header */
int play_game(int (*)(const Board&), int (*)(const Board&),
bool = false);
/* in TU */
int play_game(int (*p1)(const Board&), int (*p2)(const Board&),
bool silent /* = false */)
{
return 42;
}

V
 
T

Thomas Nelson

Read the FAQ 5.8. That's for the next time. For now, however, try
changing it to

/* in header */
int play_game(int (*)(const Board&), int (*)(const Board&),
bool = false);
/* in TU */
int play_game(int (*p1)(const Board&), int (*p2)(const Board&),
bool silent /* = false */)
{
return 42;
}

V

Thanks, this fixed my problem. Next time I will post more descriptive
code.

Tom
 
J

Jonas

Thomas Nelson said:
I'm trying to write a function that uses function pointers. Here's my
errors:
Board.cc:111: error: expected ',' or '...' before 'p1'
Board.cc:111: error: prototype for 'int Board::play_game(int (*)(const
Board&))' does not match any in class 'Board'
Board.h:18: error: candidate is: int Board::play_game(int (*)(const
Board&), int (*)(const Board&), bool)


Here's Board.cc:111:

int Board::play_game(int(const Board &) p1, int(const Board &) p2,
bool silent=false)

And here's Board.h:18:

int play_game(int(const Board &), int(const Board &), bool
silent);

What have I done wrong? I wanted to use a typedef to simplify this,
but Board has to be declared to use it in a typedef, and I want to use
the typedef in my Board declaration. Any help would be great.

Like so:

int Board::play_game(int (*p1)(const Board &), int (*p2)(const Board &),
bool silent=false);

Or, using a typedef:

class Board {
typedef int func(const Board & board);
int play_game(func * p1, func * p2, bool silent=false);
};

Alternatively,

class Board;
typedef int func(const Board & board);

class Board {
int play_game(func * p1, func * p2, bool silent=false);
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,536
Members
48,284
Latest member
alphabetsalphabets

Latest Threads

Top