Thread: Failure in file inclusion

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    The larch
    Join Date
    May 2006
    Posts
    3,573
    wxWidgets has a wxString class. You are using it in at least one place - for titulo. It has a ToLong method that might do the string to integer conversion.

    By the way, it seems that you are using the new keyword way too much. All the wxPoints, wxSizes and wxStrings should be plain static objects or at least deleted after you have done with them. It's different for windows and window controls (and some other things) where the parent will take reponsibility for the new-ed objects.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #2
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    Quote Originally Posted by anon View Post
    wxWidgets has a wxString class. You are using it in at least one place - for titulo. It has a ToLong method that might do the string to integer conversion.

    By the way, it seems that you are using the new keyword way too much. All the wxPoints, wxSizes and wxStrings should be plain static objects or at least deleted after you have done with them. It's different for windows and window controls (and some other things) where the parent will take reponsibility for the new-ed objects.
    So you mean I should do it like this? (seems to work as well; fewer "new" keywords; OnExit method calling delete on the required pointers)

    Code:
    #define CELSIUS_BUTTON_ID 1
    #define FAHRENHEIT_BUTTON_ID 2
    #include "wx/wx.h"
    
    class CelsiusFahrenheit: public wxApp {
      public:
        //Variaveis globais
        wxTextCtrl* celsius_text;
        wxTextCtrl* fahrenheit_text;
        wxTextCtrl* output_text;
        //Metodo OnInit, iniciador da aplicacao
        bool OnInit() {
          //Inicializa a janela
          int comprimento_janela = 400;
          int largura_janela = 300;
          wxSize tamanho_janela(comprimento_janela,largura_janela);
          wxString titulo("Conversor Celsius-Fahrenheit-Celsius");
          //Janela com:
          //-CAPTION (barra de titulo)
          //-CLOSE BOX (botao fechar no canto superior direito)
          //-SYSTEM MENU (conjunto de botoes minimizar, maximizar, fechar)
          /*
          NOTA: Apenas os botoes do SYSTEM MENU que forem especificados
          (neste caso, apenas a CLOSE BOX foi especificada) aparecem na barra
          superior.
          */
          long estilo_janela = wxCAPTION | wxCLOSE_BOX | wxSYSTEM_MENU;
          wxFrame* janela = new wxFrame(NULL, -1, titulo, wxDefaultPosition, tamanho_janela, estilo_janela);
          wxColour cor_de_fundo(48, 214, 100);
          janela->SetBackgroundColour(cor_de_fundo);
          //Inicializa os controlos de texto
          int celsius_text_x = 0;
          int celsius_text_y = 0;
          int fahrenheit_text_x = celsius_text_x;
          int fahrenheit_text_y = celsius_text_y + 50;
          int output_text_x = celsius_text_x;
          int output_text_y = fahrenheit_text_y + 50;
    
          wxPoint posicao_celsius_text(celsius_text_x,celsius_text_y);
          wxPoint posicao_fahrenheit_text(fahrenheit_text_x,fahrenheit_text_y);
          wxPoint posicao_output_text(output_text_x,output_text_y);
    
          wxSize tamanho_output(400,largura_janela-output_text_y);
          wxSize tamanho_texto(200, 25);
    
          celsius_text = new wxTextCtrl(janela, -1, "", posicao_celsius_text, tamanho_texto);
          fahrenheit_text = new wxTextCtrl(janela, -1, "", posicao_fahrenheit_text, tamanho_texto);
          output_text = new wxTextCtrl(janela, -1, "", posicao_output_text, tamanho_output, wxTE_READONLY);
    
          *celsius_text << "Valor em graus Celsius";
          *fahrenheit_text << "Valor em graus Fahrenheit";
          *output_text << "Aparecera aqui o resultado de uma das conversoes.";
          //Inicializa os botoes
          wxSize tamanho_botoes(200, 25);
    
          wxPoint posicao_celsius_button(celsius_text_x + tamanho_texto.GetWidth(),celsius_text_y);
          wxPoint posicao_fahrenheit_button(fahrenheit_text_x + tamanho_texto.GetWidth(),fahrenheit_text_y);
    
          wxButton* celsius_button = new wxButton(janela, CELSIUS_BUTTON_ID, "", posicao_celsius_button, tamanho_botoes);
          wxButton* fahrenheit_button = new wxButton(janela, FAHRENHEIT_BUTTON_ID, "", posicao_fahrenheit_button, tamanho_botoes);
    
          celsius_button->SetLabel("Converter Celsius para Fahrenheit");
          fahrenheit_button->SetLabel("Converter Fahrenheit para Celsius");
    
          janela->Show(true);
          return true;
        }
        //Metodo OnExit, terminador da aplicacao
        int OnExit() {
          delete celsius_text;
          delete fahrenheit_text;
          delete output_text;
          return 0;
        }
        //Tratadores de eventos
        void OnCelsiusClick(wxCommandEvent& event) {
          wxString s = celsius_text->GetValue();
          double d = 0.0;
          if (!s.ToDouble(&d)) {
            wxMessageDialog exception_dialog(NULL,"Nao foi introduzido um valor decimal!","Erro!");
            exception_dialog.ShowModal();
            return;
          }
          double resultado = d/5.0*9.0+32; //Formula para converter um valor de Celsius para Fahrenheit
          output_text->SetValue("");
          *output_text << d << " graus Celsius correspondem a " << resultado << " graus Fahrenheit.";
        }
        void OnFahrenheitClick(wxCommandEvent& event) {
          wxString s = fahrenheit_text->GetValue();
          double d = 0.0;
          if (!s.ToDouble(&d)) {
            wxMessageDialog exception_dialog(NULL,"Nao foi introduzido um valor decimal!","Erro!");
            exception_dialog.ShowModal();
            return;
          }
          double resultado = (d-32)/9.0*5.0; //Formula para converter um valor de Fahrenheit para Celsius
          output_text->SetValue("");
          *output_text << d << " graus Fahrenheit correspondem a " << resultado << " graus Celsius.";
        }
        //Instrucao necessaria para que esta classe possa tratar eventos
        DECLARE_EVENT_TABLE()
    };
    
    //Eventos da classe CelsiusFahrenheit
    BEGIN_EVENT_TABLE(CelsiusFahrenheit, wxApp)
     EVT_BUTTON(CELSIUS_BUTTON_ID, CelsiusFahrenheit::OnCelsiusClick)
     EVT_BUTTON(FAHRENHEIT_BUTTON_ID, CelsiusFahrenheit::OnFahrenheitClick)
    END_EVENT_TABLE()
    //Iniciar a aplicacao
    IMPLEMENT_APP(CelsiusFahrenheit)
    Last edited by Mr_Miguel; 06-06-2007 at 12:15 PM. Reason: Forgot to remove unnecessary includes from my code.
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM