-1

My setup is as follows:

main.cpp
projectQt.cpp
projectQt.h
tab1.h
tab1.cpp
tab2.h
tab2.cpp
projectQt.ui
ui_projectQt.h

I want to create a project with a UI file. In the UI file, I have three comboBoxes and two Apply buttons and in the tab1.cpp and tab2.cpp, I want to add the initial Items of the comboboxes. If the user clicks one of the Apply buttons, run the "OnBtnApplyClicked" methods.

However I get this error:

projectQt.obj : error LNK2019: unresolved external symbol "public: __thiscall Ctab1::~Ctab1(void)" (??1Ctab1@@QAE@XZ) referenced in function "public: __thiscall ProjectQt::ProjectQt(class QWidget *)" (??0ProjectQt@@QAE@PAVQWidget@@@Z)
C:\output\Deneme\Qt\Win32\Debug\\projectQt.exe : fatal error LNK1120: 1 unresolved externals

main.cpp:

#include "projectQt.h"

#include <QtWidgets/QApplication>
    
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    ProjectQt w;
    w.show();
    return a.exec();
}

projectQt.ui file:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>ProjectQtClass</class>
 <widget class="QMainWindow" name="ProjectQtClass">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>900</width>
    <height>900</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>ProjectQt</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QTabWidget" name="tabWidget">
    <property name="geometry">
     <rect>
      <x>10</x>
      <y>40</y>
      <width>800</width>
      <height>800</height>
     </rect>
    </property>
    <property name="currentIndex">
     <number>1</number>
    </property>
    <widget class="QWidget" name="IDC_FORM_TAB_1">
     <attribute name="title">
      <string>M001</string>
     </attribute>
     <widget class="QLabel" name="IDC_LBL_1_0">
      <property name="geometry">
       <rect>
        <x>10</x>
        <y>40</y>
        <width>124</width>
        <height>13</height>
       </rect>
      </property>
      <property name="text">
       <string>COMBO1</string>
      </property>
     </widget>
     <widget class="QComboBox" name="IDC_CMB_1_0">
      <property name="geometry">
       <rect>
        <x>120</x>
        <y>40</y>
        <width>69</width>
        <height>22</height>
       </rect>
      </property>
     </widget>
     <widget class="QComboBox" name="IDC_CMB_1_1">
      <property name="geometry">
       <rect>
        <x>120</x>
        <y>90</y>
        <width>69</width>
        <height>22</height>
       </rect>
      </property>
     </widget>
     <widget class="QPushButton" name="IDC_BTN_1_Apply">
      <property name="geometry">
       <rect>
        <x>180</x>
        <y>174</y>
        <width>75</width>
        <height>23</height>
       </rect>
      </property>
      <property name="text">
       <string>Apply</string>
      </property>
     </widget>
     <widget class="QLabel" name="IDC_LBL_1_1">
      <property name="geometry">
       <rect>
        <x>10</x>
        <y>90</y>
        <width>124</width>
        <height>13</height>
       </rect>
      </property>
      <property name="text">
       <string>COMBO2</string>
      </property>
     </widget>
    </widget>
    <widget class="QWidget" name="IDC_FORM_TAB_2">
     <attribute name="title">
      <string>M002</string>
     </attribute>
     <widget class="QLabel" name="IDC_LBL_2_0">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>30</y>
        <width>104</width>
        <height>13</height>
       </rect>
      </property>
      <property name="text">
       <string>COMBO3</string>
      </property>
     </widget>
     <widget class="QComboBox" name="IDC_CMB_2_0">
      <property name="geometry">
       <rect>
        <x>100</x>
        <y>30</y>
        <width>69</width>
        <height>22</height>
       </rect>
      </property>
     </widget>
     <widget class="QPushButton" name="IDC_BTN_2_Apply">
      <property name="geometry">
       <rect>
        <x>100</x>
        <y>70</y>
        <width>75</width>
        <height>23</height>
       </rect>
      </property>
      <property name="text">
       <string>Apply</string>
      </property>
     </widget>
    </widget>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>900</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources>
  <include location="projectQt.qrc"/>
 </resources>
 <connections/>
</ui>

projectQt.cpp file:

#include "projectQt.h"
#include "tab1.h"
#include "tab2.h"
#include "ui_projectQt.h"

ProjectQt::ProjectQt(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    
    Ctab1 ctab11;
    Ctab1 ctab22;
    ctab11.initGUI();
    ctab22.initGUI();
    
    connect(ui.IDC_BTN_1_Apply,SIGNAL(clicked()),this,SLOT(Ctab1::OnBtnApplyClicked()));
    connect(ui.IDC_BTN_2_Apply,SIGNAL(clicked()),this,SLOT(Ctab2::OnBtnApplyClicked()));

}

ProjectQt::~ProjectQt()
{

}

projectQt.h file:

#ifndef projectQt_H
#define projectQt_H

#include <QtWidgets/QMainWindow>
#include "ui_projectQt.h"
#include "tab1.h"
#include "tab2.h"

class ProjectQt : public QMainWindow
{
    Q_OBJECT

public:
    ProjectQt(QWidget *parent = 0);
    ~ProjectQt();

//private:
    Ui::ProjectQtClass ui;
};

#endif // projectQt_H

tab1.h file:

#pragma once

#include <string>
#include "projectQt.h"
#include "ui_projectQt.h"

class Ctab1
{
    
public:
    Ctab1(void);
    ~Ctab1(void);

public slots:
    void setEvents();
    void initGUI();
    void OnBtnApplyClicked();

//private:
    //Ui::ProjectQtClass *ui;
    
};

tab1.cpp file:

#include "projectQt.h"
#include "tab1.h"
#include <QTextStream>
#include "ui_projectQt.h"

Ctab1::Ctab1(void)
{
}

void Ctab1::initGUI(){
    Ui::ProjectQtClass uimain;
    uimain.IDC_CMB_1_0->addItem("1100");
    uimain.IDC_CMB_1_0->addItem("1101");
    uimain.IDC_CMB_1_1->addItem("1200");
    uimain.IDC_CMB_1_1->addItem("1201");
    
}

void Ctab1::OnBtnApplyClicked(){
    
}

tab2.h file:

#pragma once

#include <string>
#include "projectQt.h"
#include "ui_projectQt.h"

class Ctab2
{
    
public:
    Ctab2(void);
    ~Ctab2(void);

public slots:
    void setEvents();
    void initGUI();
    void OnBtnApplyClicked();
    
//private:
    //Ui::ProjectQtClass *ui;
    
};

tab2.cpp file:

#include "projectQt.h"
#include "tab2.h"
#include <QTextStream>
#include "ui_projectQt.h"

Ctab2::Ctab2(void)
{
}

void Ctab2::initGUI(){
    
    Ui::ProjectQtClass uimain;
    uimain.IDC_CMB_2_0->addItem("2000");
    uimain.IDC_CMB_2_0->addItem("2001");
    
}

void Ctab2::OnBtnApplyClicked(){
        
}
rgettman
  • 176,041
  • 30
  • 275
  • 357
CiTi
  • 1
  • 1

1 Answers1

2

Whenever you see an error along the lines of "undefined reference ...", it means it's a linker error.

This particular error means that somewhere in your program, the destructor for the Ctab1 class is being called but it can't find the implementation of that destructor in any of your source files, only the declaration.

Add the following to your tab1.cpp file:

Ctab1::~Ctab1(){
   // any cleanup code placed here
}

It also seems clear that you don't fully understand variable scope in C++. (ctab11 and ctab22 only exist inside the constructor of ProjectQt

I suggest you take an introductory course in C/C++ before carrying on with your project.

RobbieE
  • 4,280
  • 3
  • 22
  • 36