FLTK logo

Re: [fltk.general] FLTK usage in passing pointers from file to file

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.general  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: FLTK usage in passing pointers from file to file Greg Ercolano Feb 25, 2021  
 


On 2/25/21 5:19 PM, John E. Kaye P.Eng. wrote:
Thank you for your response and comment. I had declared the Window pointers Globally such as Fl_Window *w1, Fl_Window *w2 etc BUT defined them in the main() program in FILE 1 as Fl_Window w1(5,198,600,275,"Tester"), etc

    This is bad; sounds like you're declaring two different w1 variables; one global.

    Functions outside main() will only see the uninitialized global pointer variable 'w1',
    not the initialized one inside main().

    Let's ignore w2, and just focus on w1.  Sounds like you have this:

Fl_Window *w1;          // OK: declares global pointer variable w1
int main() {
    Fl_Window w1(...);  // BAD: declares a separate local variable w1 that shadows the global one
    ..
}

     Other functions will only see the global 'w1' which is uninitialized by your code,
     and is causing the crashes.

     What you should do instead is only declare 'w1' once, as a global,
     then set it in main() using the code in green:

// main.cxx
Fl_Window *w1 = 0;   // declare global pointer w1, init to null

int main(..) {
    // Create window w1, saving pointer to created window in w1
    w1 = new Fl_Window(5,198,600,275,"Tester");

    // define widgets that go inside w1..

    w1->end();

   // From here, you can call your functions in your other file
   // that use w1 and it should work

   return Fl::run();  // application loop
}

    That should work correctly.

Any further comments??

    Yes, be careful of variable scoping in C and C++.

    It's possible to have many variables that are completely different, but all
    have the same name, so be careful of that. I believe that's what bit you here.

    And when working with pointers to FLTK widgets, you want to create the widgets
    with this pattern:

        Fl_Some_Widget *wa = new Fl_Some_Widget(...);
        Fl_Other_Widget *wb = new Fl_Other_Widget(...);

   Note the use of 'new' to create an instance of the widget's class, and saving its
   value in the pointer.

    Some widgets are containers, like windows and groups, so you need to use
    begin() and end() when creating widgets that go inside those containers.

    I suggest having a look at this beginner's tutorial for programming in FLTK:
    https://www.youtube.com/watch?v=vW626w2Ipyc

    ..and also how to use 'fluid', the gui interface designer:

    https://www.youtube.com/watch?v=U83xH1KZ_is


--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/3273f9b1-fcbe-cce8-b7d5-dca3d9da6822%40seriss.com.
Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.