Simplified the code to make it easier to deal with the problem. There is a class described in mainwindow.h:

#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QFormLayout> #include <QLabel> #include <QLineEdit> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); public slots: void Menu1(); void NZER_N_B(int &n); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H 

The NZER_N_B (int & n) function should be implemented in a separate .cpp file; it is called in the main file.

nzer_n_b.cpp:

 #include "mainwindow.h" void MainWindow::NZER_N_B(int &n) { n = 1770; } 

mainwindow.cpp:

 #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->pb1, SIGNAL(clicked(bool)), this, SLOT(Menu1())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::Menu1() { int n; NZER_N_B(n); } 

But with this implementation errors LNK1120,2001,2019 appear (reference to an unresolved element). But if you write the implementation of this function in the header file and connect it accordingly. That is no error, but not so right, as far as I know. So, the question is how to shove the implementation of the code in cpp and so that errors do not occur?

  • writing the implementation of a function in the h file is normal (of course, situations are different and sometimes it is not normal). Show the whole project that does not compile and I think you will be offered a solution. - KoVadim
  • And you have not forgotten to connect the newly created cpp-file to the project? - gbg
  • in the test.pro file they are connected: SOURCES + = main.cpp \ mainwindow.cpp \ nzer_n_b.cpp - Dmitry Subbotin
  • @DmitrySubbotin At least you forgot to turn it on. And why do you need a class function if it does not use anything from this ??? Can declare it simply as a function? - Sublihim
  • @Sublihim "At a minimum, you forgot to turn it on" - can you talk about this in more detail, please. Just in qt I work for the first time. In MCVS, it was possible to declare functions in the header file and simply write the implementation in at least 10 cpp. The class function is needed, as the code will be added further. This example is purely to understand - Dmitry Subbotin

2 answers 2

It is necessary to understand how the preprocessor works. When it meets the #include directive, it simply replaces it with the code from the specified file. Therefore, if you really wanted to split the implementation into several pairs of files, then you need to do the following:

nzer_n_b.cpp:

 void MainWindow::NZER_N_B(int &n) { n = 1770; } 

mainwindow.cpp:

 #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect(ui->pb1, SIGNAL(clicked(bool)), this, SLOT(Menu1())); } MainWindow::~MainWindow() { delete ui; } // Можно и вначале файла, под всеми другими #include // но тут, просто, для примера #include "nzer_n_b.cpp" void MainWindow::Menu1() { int n; NZER_N_B(n); } 

    If everything is exactly as described in the example, then there should be no problems. Perhaps after adding the file, qmake was not called to regenerate the Makefiles. Then you need to call qmake from the context menu of the project, and rebuild the project.