2015-01-10 2 views
0

문제가 며칠 동안 붙어 있습니다. 나는 gtk를 포함하는 프로젝트 할당을 위해 C++를 배워야했다. 나는 원하는 윈도우를 프로그래밍하고 대부분의 기능을 다루었지만 콜백 함수에서 데이터를 저장하는 방법을 고민 할 때마다 머리를 감쌀 수 있습니다!gtk의 calllback 기능에서 함수 호출

계속하기 전에 인터넷 검색을 통해 답변을 찾았지만 동일한 문제가 발생하지 않았습니다. 나는

어쨌든, (다른 사람의 사이에서) 3 개 차 클래스가 포함되는 정적 콜백 함수에서 함수를 호출 할 수 있다는 사실을 알고 :

  • 창 - 위젯 만 책임, 버퍼에 쓰기 .
  • 키보드 - 사용자 입력을 듣고 상태 메시지를 업데이트하는 루프가 있으며 버퍼에서 읽습니다.
  • 버퍼 - 사용자가 버퍼에 데이터를 추가합니다. Window 클래스는 버퍼에 쓸 수 있어야하며 키보드는 읽을 수 있어야합니다.

특정 시점에서 프로그램을 실행할 때 사용자는 새 좌표를 입력하려고합니다. 특정 버튼을 누르고 클래스 내에서 Window 클래스 함수가 ​​호출되고 입력 대화 상자가 표시되면 키보드 리스너에서 들립니다.

Window *window; //Header 
.... 
void Keyboard::GetInputCoord(){ //.cpp 
    window->display(); 
} 

창은이 같은 Keyboard.h에서 참조하는 기본 클래스에 연결되어 있습니다 :

.... 
keyboard.window = &window; 
keyboard.buff_read = &buff; 
window.buff_write = &buff; 
.... 

내가 Window 클래스, 디스플레이(), 배열을 보낼 manged했던에 있습니다 스태틱 콜백 함수에 대한 내 항목을 가져 왔지만 이제 이것을 내 버퍼로 보내는 방법을 알아내는 데 어려움이 있습니까? 나는 ... 같은 클래스에서 함수를 호출하지만, 쓸모없는 시도

static void entry_coord(GtkButton *widget, GtkWidget **entry)    
{                   
    GtkWidget *entry_x_in = entry[0];         
    GtkWidget *entry_y_in = entry[1];         
    const char *x, *y;             

    x = gtk_entry_get_text(GTK_ENTRY(entry_x_in));      
    y = gtk_entry_get_text(GTK_ENTRY(entry_y_in));      

    // write_buff(x, y); <- something like this 
} 

나는이 (가) Window.h에 선언하고 내가해야합니까 어떤 옵션을 cpp를 구현 한이 함수를 호출 할 수 없기 때문에? 해결 방법이 있습니까?

편집 2015년 1월 10일

Window.h

#ifndef def_Window 
#define def_Window 

#include <stdio.h> 
#include <stdlib.h> 
#include <string> 
#include <ncurses.h> 
#include <gtk/gtk.h> 

#include "Buffer.h" 

using namespace std; 

class Window 
{ 
    public: 
      Window(); 
      void display(); 
      void write_buff(const char, const char); 

      Buffer *buff_write; //Sound maybe be in private 
    private: 

}; 

Buffer.h

#ifndef def_buffer 
#define def_buffer 

#include <stdio.h> 
#include <stdlib.h> 
#include <string> 

#define BUFF_ROW 10 
#define BUFF_COL 2 

using namespace std; 

class Buffer 
{ 
    public: 
      Buffer(); 
      void write_to_buffer(string msg); 
      void read_from_buffer(); 
    private: 
      int front, back, size, count; 
      string buffer[][]; 
}; 
#endif 

Keyboard.h

#ifndef def_Keyboard 
#define def_Keyboard 

#include <math.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string> 
#include <boost/filesystem.hpp> 
#include <boost/thread.hpp> 
#include <gtk/gtk.h> 

#include "Kinematic.h" 
#include "Window.h" 
#include "Buffer.h" 

#define MSG 10 

using namespace std; 

class Keyboard 
{ 
    public: 
      Keyboard(); 
      void UpdateMessages(); 
      void SetStatus(string msg); 
      void SetMessage(string msg); 
      string messages[10]; 
      int indexMsg; 
      string statusString; 
      .... 

      Window *window;  
      Buffer *buff_read; 

    private: 
}; 

나는 트라이했습니다 버퍼에 여러 가지 방법으로 기록 할 수 있습니다. 나는 버퍼 함수 write_to_buffer()를 호출 해 보았고 write_to_buffer를 호출하는 write_buff() 함수를 호출 해 보았습니다. 컴파일러는 함수가이 범위에서 선언되지 않는다고 말합니다.

+0

이와 같이'write_buff()'를 호출하면 어떻게됩니까? 최소한 헤더 파일을 제공 할 수 있습니까? – andlabs

+0

첫 글을 수정했습니다 –

+0

'entry' 클래스 (')는'Window' 클래스의 일부입니까? 그렇지 않다면 어떻게 든'Window'를 주어야합니다.'Window'의 메소드 인 write_buff()를 호출 할 수 있습니다. – andlabs

답변

0

entry_coord()에 대한 userdata 포인터를 사용자가 해당 기능에서 필요한 모든 항목에 액세스 할 수 있도록하는 것으로 해결할 수 있습니다. 항목 및 버퍼에 대한 포인터를 가지고 구조체의 당신의 아이디어는 확실히 작동하지만 그것은 내게도 귀하의 Window 클래스뿐만 아니라 작동해야합니다 ...그냥 데이터 포인터로 this를 사용하고이 같은 콜백 보면 뭔가합니다

static void entry_coord(GtkButton *widget, gpointer data_ptr) 
{ 
    auto window = static_cast<Window*> (data_ptr); 
    window->update_buff_from_entries(); 
} 

그런 다음 창 클래스는 자연스럽게 새로운 공개 방법 update_buff_from_entries() 그들로부터 값을 얻을 수 있도록 항목에 (개인) 포인터를 포함 할 필요를 write_buff()으로 전화하십시오.