0

我创建了一个包含 4 个标签的应用程序。我想更改两个标签(“wynik”和“wynik1”)的值。当我按下键盘上的左键时,“wynik”标签的值应该增加,当我按下键时减少。当我按下右键时,“wynik1”标签的值应该增加,当我按下键时减少。我的代码:

    #include <gtk/gtk.h>
    #include <gdk/gdkkeysyms.h>

    gint count = 0;
    char buf[0];
    char buf1[0];


    gboolean
    on_key_press (GtkWidget *widget, GdkEventKey *event, gpointer wynik);
    gboolean
    on_key_press1 (GtkWidget *widget, GdkEventKey *event, gpointer wynik);

    int main( int argc, char * argv[] ) {
        GtkWidget * okno;
        GtkWidget * kontener;
        GtkWidget * etykieta;
        GtkWidget * etykieta1;
        GtkWidget * wynik;
        GtkWidget * wynik1;

        gtk_init( & argc, & argv );

        okno = gtk_window_new( GTK_WINDOW_TOPLEVEL );
        gtk_window_set_keep_above(GTK_WINDOW(okno), TRUE);
        gtk_window_set_default_size( GTK_WINDOW( okno ), 300, 200 );
        gtk_window_set_position( GTK_WINDOW( okno ), GTK_WIN_POS_CENTER );

        kontener = gtk_fixed_new();
        gtk_container_add( GTK_CONTAINER( okno ), kontener );

        char * str = " <span fgcolor='#007560'><span font='20'><b>PIA</b></span></span>";
        etykieta = gtk_label_new( NULL );
        gtk_label_set_markup( GTK_LABEL( etykieta ), str );
        gtk_fixed_put( GTK_FIXED( kontener ), etykieta, 0, 0 );

        char * str1 = " <span fgcolor='#9a1262'><span font='20'><b>PER</b></span></span>";
        etykieta1 = gtk_label_new( NULL );
        gtk_label_set_markup( GTK_LABEL( etykieta1 ), str1 );
        gtk_fixed_put( GTK_FIXED( kontener ), etykieta1, 65, 0 );

        char * wyn = " <span fgcolor='#007560'><span font='26'><b>0</b></span></span>";
        wynik = gtk_label_new( NULL );
        gtk_label_set_markup( GTK_LABEL( wynik ), wyn );
        gtk_fixed_put( GTK_FIXED( kontener ), wynik, 3, 30 );

        char * wyn1 = " <span fgcolor='#9a1262'><span font='26'><b>0</b></span></span>";
        wynik1 = gtk_label_new( NULL );
        gtk_label_set_markup( GTK_LABEL( wynik1 ), wyn1 );
        gtk_fixed_put( GTK_FIXED( kontener ), wynik1, 68, 30 );

        g_signal_connect( G_OBJECT( okno ), "destroy", G_CALLBACK( gtk_main_quit ), NULL );
        g_signal_connect (G_OBJECT (okno), "key_press_event", G_CALLBACK (on_key_press), wynik);
        g_signal_connect (G_OBJECT (okno), "key_press_event", G_CALLBACK (on_key_press1), wynik1);
        gtk_widget_show_all( okno );
        gtk_main();

        return 0;
    }

    gboolean
    on_key_press (GtkWidget *widget, GdkEventKey *event, gpointer wynik)
    {
    switch (event->keyval)
      {case GDK_S:
        case GDK_Left:
            count++;
            std::ostringstream os;
            os << "<span font='26'><b>" << count << "</b></span>";
            gtk_label_set_markup( GTK_LABEL( wynik ), os.str().c_str() );
          break;
        case GDK_Down:
            count--;
            std::ostringstream os;
            os << "<span font='26'><b>" << count << "</b></span>";
            gtk_label_set_markup( GTK_LABEL( wynik ), os.str().c_str() );
          break;

        default:
          return FALSE;
      }    
      return FALSE;
    }

 gboolean
    on_key_press1 (GtkWidget *widget, GdkEventKey *event, gpointer wynik1)
    {
    switch (event->keyval)
      {case GDK_S:  // add this line
        case GDK_Right:
            count++;
            std::ostringstream os1;
            os1 << "<span font='26'><b>" << count << "</b></span>";
            gtk_label_set_markup( GTK_LABEL( wynik1 ), os1.str().c_str() );
          break;
        case GDK_Up:
            count--;
            std::ostringstream os1;
            os1 << "<span font='26'><b>" << count << "</b></span>";
            gtk_label_set_markup( GTK_LABEL( wynik1 ), os1.str().c_str() );
          break;

        default:
          return FALSE;
      }    
      return FALSE;
    }

不幸的是我收到错误:

> ||=== nowygt3, Debug ===| F:\c++\nowygt3\main.c||In function
> 'on_key_press':| F:\c++\nowygt3\main.c|67|error: expected expression
> before ':' token| F:\c++\nowygt3\main.c|68|error: 'os' undeclared
> (first use in this function)| F:\c++\nowygt3\main.c|68|note: each
> undeclared identifier is reported only once for each function it
> appears in| F:\c++\nowygt3\main.c|73|error: duplicate label 'std'|
> F:\c++\nowygt3\main.c|67|note: previous definition of 'std' was here|
> F:\c++\nowygt3\main.c|73|error: expected expression before ':' token|
> F:\c++\nowygt3\main.c|67|warning: label 'std' defined but not used
> [-Wunused-label]| F:\c++\nowygt3\main.c||In function 'on_key_press1':|
> F:\c++\nowygt3\main.c|91|error: expected expression before ':' token|
> F:\c++\nowygt3\main.c|92|error: 'os1' undeclared (first use in this
> function)| F:\c++\nowygt3\main.c|97|error: duplicate label 'std'|
> F:\c++\nowygt3\main.c|91|note: previous definition of 'std' was here|
> F:\c++\nowygt3\main.c|97|error: expected expression before ':' token|
> F:\c++\nowygt3\main.c|91|warning: label 'std' defined but not used
> [-Wunused-label]|

请帮我。我真的是初学者,但我尝试自己做所有事情,现在我没有想法。

4

2 回答 2

0

似乎在抱怨 std::ostringstream os1;

你有#include <iostream>吗?

于 2013-03-29T20:06:52.173 回答
0

case要在特定的switch语句中创建具有自动存储持续时间的对象:

case GDK_S:
case GDK_Left:
    std::ostringstream os;
    ...
    break;

您应该定义范围,其生命周期os将与:

case GDK_S:
case GDK_Left:
{
    std::ostringstream os;
    ...
    break;
}

除此之外,您还#include <sstream>应该能够使用std::ostringstream.

于 2013-03-29T20:09:01.183 回答