undefined reference to `Class::Class()'

11,021

Undefined reference errors mean you either forgot to write define the missing function (by writing an implementation in the .cpp file), or you forgot to link the appropriate object file or library into the final binary.

In this case, it's the later reason. You need to include MainWindowPane.o in the linker command in your makefile:

g++  -g -o QPI_frontend main.o MainWindow.o StartButton.o `pkg-config gtkmm-2.4 --libs`
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                        you need MainWindowPane.o in here
Share:
11,021
ILikeFood
Author by

ILikeFood

Updated on June 04, 2022

Comments

  • ILikeFood
    ILikeFood almost 2 years

    I am writing a GTKmm window program; the main window creates two buttons, one for English and one for Chinese. The user can click on the button to bring up a different window in the appropriate language. Currently I am having trouble initializing the multiple-item container inside the main window. It is an object of type MainWindowPane, which inherits Gtk::HBox.

    When I try to make, the compiler issues the following error:

    $ make 
    g++ -g `pkg-config gtkmm-2.4 --cflags` -c MainWindow.cpp  
    g++  -g -o QPI_frontend main.o MainWindow.o StartButton.o `pkg-config gtkmm-2.4 --libs`  
    MainWindow.o: In function `MainWindow':  
    /home/dmurvihill/Documents/QPI_frontend/MainWindow.cpp:9: undefined reference to   `MainWindowPane::MainWindowPane()'  
    /home/dmurvihill/Documents/QPI_frontend/MainWindow.cpp:9: undefined reference to  `MainWindowPane::MainWindowPane()'  
    collect2: ld returned 1 exit status  
    make: *** [QPI_frontend] Error 1  
    

    I am using the latest version of gcc with pkg-config to include the proper libraries. I am also a java person.

    /*
     * MAIN_WINDOW.H
     * Responsible for creating "slave" RSED windows. Can create English or Chinese
     * versions of the demo, and can destroy them all with one click.
     */  
    #ifndef MAINWINDOW_H  
    #define MAINWINDOW_H  
    
    #include <gtkmm/window.h>
    
    //#include "SlaveWindow.h"
    #include "StartButton.h"
    #include "MainWindowPane.h"
    
    class MainWindow : public Gtk::Window
    {
    public:
        MainWindow();   
    private:
        MainWindowPane pane;
    //  std::list<SlaveWindowThread> windows;       // Keeps track of all windows that have been created thus far.
        void destroyAllWindows();           // Iterates through the linked list and destroys each window.
    };
    #endif      //ifndef MAINWINDOW_H
    

    /* 
     * MAIN_WINDOW.CPP
     *
     */  
    #include "MainWindow.h"  
    #include "MainWindowPane.h"  
    #include "StartButton.h"  
    
    MainWindow::MainWindow()// : /*list,*/ pane(/*list*/)
    {
        pane;
    }
    
    void MainWindow::destroyAllWindows()
    {
        //gtk_widget_destroy(*this);
        // TODO: Destroy all the other windows too.
    }
    

    /*
     * MAIN_WINDOW_PANE.H
     */  
    #ifndef MAINWINDOWPANE_H  
    #define MAINWINDOWPANE_H  
    
    #include <gtkmm/box.h>
    #include <gtkmm/button.h>
    
    //#include "SlaveWindow.h"
    #include "StartButton.h"
    
    class MainWindowPane : public Gtk::HBox
    {
    public:
        MainWindowPane(/*&(std::list)*/);   
    private:
        StartButton englishButton;      // Used to create a new RSED demo screen.
        StartButton chineseButton;      // Used to create a new RSED demo in chinese.
    //  std::list<SlaveWindow> windows;     // Keeps track of all windows that have been created thus far.
        void destroyAllWindows();       // Iterates through the linked list and destroys each window.
    };
    #endif      //ifndef MAINWINDOWPANE_H
    

    /* 
     * MAIN_WINDOW.CPP
     *
     */  
    #include "MainWindowPane.h"  
    #include "StartButton.h"  
    
    MainWindowPane::MainWindowPane(/*&(std::list)*/) : 
        englishButton(StartButton::ENGLISH/*,&(std::list)*/), 
        chineseButton(StartButton::CHINESE/*,&(std::list)*/)
    {
        pack_start(englishButton);
        englishButton.show();
        pack_start(chineseButton);
        chineseButton.show();
    }
    
    void MainWindow::destroyAllWindows()
    {
        //gtk_widget_destroy(*this);
        // TODO: Destroy all the other windows too.
    }
    
  • ILikeFood
    ILikeFood over 13 years
    I forgot to put in my code at first. Oops! I've edited that in now. You can see that both the constructors I have defined the default constructor for MainWindowPane in the cpp file. Hope the code makes it a little clearer. I feel like I'm making some trivial mistake on the syntax somewhere (something that's easy for Java programmers to slip into!).
  • ILikeFood
    ILikeFood over 13 years
    That line was an attempt to call the default constructor for pane. I encounter a different error with pane(); I moved the attempted constructor call into the function body because I encountered the same error when I tried to call pane in the initializer list. I subsequently forgot to move it back. I get the same error with this code: ... MainWindow::MainWindow() : pane() { } . With this code: MainWindow::MainWindow() : pane {} ... I get this error: MainWindow.cpp:9: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x' ... Thanks for all your help so far.
  • ILikeFood
    ILikeFood over 13 years
    That's it. Added the source to my makefile, and the code compiled successfully. Thanks! Now, on to runtime debugging...
  • Ken Bloom
    Ken Bloom over 13 years
    @ILikeFood, remember to hit the checkmark next to the answer that worked for you to accept the answer.
  • ILikeFood
    ILikeFood over 13 years
    Ahh, thank you. I tried to vote it up, too, but my acct is too new.