0

When I compile main.cpp I get an undefined reference to a class::function() and I cant seem to find what the issue is at all, I had all my code in one cpp file and it worked fine but I separated the functionality into a new header file with a corresponding cpp file that includes the header file

When I create an object (MainWindow m;) and compile that I dont get errors, but when I add m.init() thats when I get the undedfined reference to MainWindow::init();

mainWindow.h:

#pragma once
#include <gtkmm.h>
#include <string>
#include <fstream>

class MainWindow {
    protected:
        Gtk::Window *main_window;
        Gtk::Button *text_button;
        Gtk::TextView *text_view;
        Gtk::Dialog *file_dialog;
        Gtk::Entry *file_entry;
        Gtk::Button *file_button;

        std::string file;

    public:
        void init();
        void open_dialog();
        void get_entry();
};

mainWindow.cpp:

#include "mainWindow.h"


void MainWindow::get_entry() {
    file = file_entry->get_text();
    file_dialog->close();
}

void MainWindow::open_dialog() {
    std::string newText, line, file;

    file_dialog->run();
    file_button->signal_clicked().connect(sigc::ptr_fun(&get_entry));

    std::ifstream textFile(file);

    while (getline(textFile, line)) {
        newText += line;
        newText += " ";
    } 
    text_view->get_buffer()->set_text(newText);
}

void MainWindow::init() {

    Gtk::Main app();
    auto builder = Gtk::Builder::create_from_file("test.glade");

    builder->get_widget("main_window", main_window);
    builder->get_widget("text_button", text_button);
    builder->get_widget("text_view", text_view);
    builder->get_widget("file_dialog", file_dialog);
    builder->get_widget("file_entry", file_entry);
    builder->get_widget("file_button", file_button);

    text_button->signal_clicked().connect(sigc::ptr_fun(&open_dialog));

    Gtk::Main::run(*main_window);
}

main.cpp:

#include "mainWindow.h"

#include <iostream>

int main() {

    MainWindow m;
    m.init();
}
KJ465
  • 33
  • 5
  • You are not compiling and/or linking the `mainWindow.cpp` file. Please explain how you are building your program. – walnut Apr 05 '20 at 13:15
  • g++ main.cpp -o main `pkg-config gtkmm-3.0 --cflags --libs – KJ465 Apr 05 '20 at 13:19
  • Duplicate of [Using G++ to compile multiple .cpp and .h files](https://stackoverflow.com/questions/3202136/using-g-to-compile-multiple-cpp-and-h-files). In short: You need to list all `.cpp` files on the compiler command line, not only `main.cpp`. – walnut Apr 05 '20 at 13:20
  • Ahh okay thank you, yes its compiled now, – KJ465 Apr 05 '20 at 13:23

0 Answers0