How to reach any class members from Mainwindow class?
-
I have a QT Widget application. I
I want to reach ismet class members from mainwindow.cpp file
In mainwindow.cpp#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { is=new ismet(); is->My_Func(2); } MainWindow::~MainWindow() { delete ui; }
In mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include "ismet.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: ismet *is; explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
In ismet.cpp
ismet::ismet() { My_Func(2); } void ismet::My_Func(int x){ qDebug()<<x; }
In ismet.h
#ifndef ISMET_H #define ISMET_H #include<QDebug> #include<QObject> #include<QString> #include<QMainWindow> class ismet : public QObject { public: ismet(); void My_Func(int x);//declaration QString myname; }; #endif // ISMET_H
My error is : error: undefined reference to
ismet::ismet()' undefined reference to
ismet::My_Func(int)'How to solve this issue?
-
Please post your build system configuration (CMakeLists.txt file , or .pro file). Are you actually compiling and linking ismet.cpp into the executable?
-
#------------------------------------------------- # # Project created by QtCreator 2022-06-27T14:19:26 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = calisma TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which has been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ mainwindow.cpp \ ismet.cpp HEADERS += \ mainwindow.h \ ismet.h FORMS += \ mainwindow.ui
-
This looks correct to me. Can you also post the full compiler output (not just the error message, but everything in Qt Creator under 'Compile Output')?
-
#------------------------------------------------- # # Project created by QtCreator 2022-06-27T14:19:26 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = calisma TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which has been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ mainwindow.cpp \ ismet.cpp HEADERS += \ mainwindow.h \ ismet.h FORMS += \ mainwindow.ui