-2

To investigate another problem (free() error at program end), I tried to override the destructor of the FLTK Fl_Input class. The code compiles but fails in the link with an undefined reference.

I have looked at a number of examples but am not understanding the answers enough to know what I need to change to fix the problem. While this program will not reproduce the free() problem, if the Fl_Input objects (and Fl_Output objects) produce messages, I can determine which one is being freed invalidly.

#include <FL/Fl.H>
#include <FL/Fl_Input.H>
#include <iostream>


class Fl_Inputc:public Fl_Input
{
  public:
    Fl_Inputc();
    Fl_Inputc(int left, int up, int width, int height, const char* label=0);
    ~Fl_Inputc()
    {
//   std::cout <<  " Inputc destroyed " << std::endl;
    };
};

Fl_Inputc input1( 90, 10, 180, 20, "     Input : ");

int main(int argc, char **argv) {
  return Fl::run();
}

I expected a clean compile and link with a modified destructor and everything else inherited but instead got:

cbc:~/Projects/fltk/Tut/Potthast$ fltk-config --compile 07example4b.cxx 
g++ -I/usr/include/cairo -I/usr/include/glib-2.0
-I/usr/lib/x86_64-linux-gnu/glib-2.0/include 
-I/usr/include/pixman-1 -I/usr/include/freetype2 
-I/usr/include/libpng12 -I/usr/include/freetype2 
-I/usr/include/cairo -I/usr/include/glib-2.0 
-I/usr/lib/x86_64-linux-gnu/glib-2.0/include 
-I/usr/include/pixman-1 -I/usr/include/freetype2 
-I/usr/include/libpng12 -g -O2 -fvisibility-inlines-hidden 
-D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT 
-o '07example4b' '07example4b.cxx' -Wl,-Bsymbolic-functions -lfltk 
-lX11
/tmp/cc423i4F.o: In function `__static_initialization_and_destruction_0':
/Projects/fltk/Tut/Potthast/07example4b.cxx:17: undefined reference 
to `Fl_Inputc::Fl_Inputc(int, int, int, int, char const*)' 
collect2: error: ld returned 1 exit status

To resolve this using the suggestion from @alter igel I used a using statement to get the constructors from the base class.

#include <FL/Fl.H>
#include <FL/Fl_Input.H>
#include <iostream>


class Fl_Inputc:public Fl_Input
{
    public:
    using Fl_Input::Fl_Input;
    ~Fl_Inputc()
    {
     std::cout <<  " Inputc destroyed " << std::endl;
        };
};

Fl_Input  input0( 90, 10, 180, 20, "     Input0: ");
Fl_Inputc input1( 90, 40, 180, 20, "     Input1: ");

int main(int argc, char **argv) {
  return Fl::run();
}
cbcalvin
  • 25
  • 1
  • 4
  • 1
    You need to actually _define_ (implement/write a function body for) `Fl_Inputc(int, int, int, int, const char*);`at some point before your code can call that function. – alter_igel May 15 '19 at 22:08
  • How do I get Fl_Inputc to inherit the functionality defined by Fl_Input (except for the destructor)? That is the question that is the question I am asking. – cbcalvin May 16 '19 at 12:00
  • that reads like a different question altogether from what you’ve posted. You’ve _declared_ your own constructors inside your class, which prevents those of the base class from being used. If you simply remove them, you should expect the base class’ constructors to be inherited – alter_igel May 16 '19 at 16:11
  • Thanks @alter I got to the above example trying to get by a mismatch in the number of arguments. I will post a new question with that example. – cbcalvin May 16 '19 at 17:42

1 Answers1

1

The linker can't find the definition for Fl_Inputc(int left, int up, int width, int height, const char* label=0); because you haven't defined it, you've only declared it.

cj.vaughter
  • 248
  • 2
  • 6
  • Thanks @cj.vaughter. And I declared it to try and get a matching function profile. The real solution to the problem (even though I asked the wrong question) is to inherit the construtors from the base class with the using-declaration `using Fl_Input::Fl_Input;` to inherit the constructors. See code added above. – cbcalvin May 17 '19 at 21:18
  • Ah, I see. Difficult to determine what exactly you were trying to do. Glad you got it figured out. – cj.vaughter May 17 '19 at 21:24