Results 1 to 3 of 3

Thread: proble with signals and slots

  1. #1
    Serf
    Join Date
    Jun 2008
    Location
    Poland
    Posts
    2

    Default proble with signals and slots

    Hi,

    I've got some problem with connecting signal to slot in my program

    I want handleMousePress method from class Game to be called when mouse button is pressed :

    Code:
    Game::Game(CL_InputContext *ic) {
    	loadGraphics();
    
    	CL_InputDevice mouse = ic->get_mouse();
    	mousePress = mouse.sig_key_down().connect(&Game::handleMousePress);
    
    	alive = true;
    }
    here is the handleMousePress method
    Code:
    void Game::handleMousePress(const CL_InputEvent& event) {
    
    	int x = event.mouse_pos.x;
    	int y = event.mouse_pos.y;
    	int quitX = 300;
    	int quitY = 400;
    	int quitWidth = 85;
    	int quitHeight = 20;
    
    	if (x >= quitX && x <= quitX + quitWidth && y >= quitY && y <= quitY + quitHeight) {
    		std::cout << "quit" << std::endl;
    		alive = false;
    	}
    }
    When I try to compile it I've got an error:
    Code:
    Game.cpp: In constructor ‘Game::Game(CL_InputContext*)’:
    Game.cpp:7: error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function.  Say ‘&Game::handleMousePress’
    Game.cpp:7: error: no matching function for call to ‘CL_Signal_v1<const CL_InputEvent&>::connect(void (Game::*)(const CL_InputEvent&))’
    /usr/local/include/ClanLib-0.8/ClanLib/Core/System/../../Signals/signal_v1.h:172: note: candidates are: CL_Slot CL_Signal_v1<PARAM1>::connect(CL_Slot_v1<PARAM1>*) [with PARAM1 = const CL_InputEvent&]
    /usr/local/include/ClanLib-0.8/ClanLib/Core/System/../../Signals/signal_v1.h:180: note:                 CL_Slot CL_Signal_v1<PARAM1>::connect(void (*)(PARAM1)) [with PARAM1 = const CL_InputEvent&]
    make: *** [GameGame.o] Error 1

    I'm using clanLib-0.8.1 on Ubuntu 7.10

    I know the problem is that I'm giving connect function a pointer to method instead of a pointer to function, but I don't know any other way to do something like this. Can anyone help me?

    thanks
    Last edited by piotrek; 06-27-2008 at 11:30 AM.

  2. #2
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    554

    Default

    I believe the line should say:

    Code:
    mousePress = mouse.sig_key_down().connect(this, &Game::handleMousePress);
    For the signal to be able to call a member function, it must know what value the 'this' pointer should have in that function.

  3. #3
    Serf
    Join Date
    Jun 2008
    Location
    Poland
    Posts
    2

    Default

    It works

    Thanks!!

Similar Threads

  1. signals & threads
    By wamma in forum Official ClanLib SDK Forums
    Replies: 1
    Last Post: 04-18-2008, 10:34 PM
  2. Connect to signals
    By void_kill in forum Official ClanLib SDK Forums
    Replies: 2
    Last Post: 04-11-2007, 02:23 PM
  3. Slots and Keyboard
    By azizmandar in forum Official ClanLib SDK Forums
    Replies: 2
    Last Post: 01-03-2007, 03:19 AM

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •