-2

I have seen some similar questions to mine on here, but I am still having issues.

I have a button in QT, I have a function defined in my main.cpp file. When I push the QT button I want to call the function in main.cpp and let that function do its thing.

mainwindow.cpp:

void MainWindow::on_StartMotor_clicked()
{
    SendCmd(100);
}

main.cpp:

void SendCmd(INT Value)
{
}

But I get an error:

error: 'SendCmd' was not declared in this scope
     SendCmd(100);
                                             ^

Im new to QT so I dont think I fully understand the slots and signals thing.

Thanks!

sci-guy
  • 2,394
  • 4
  • 25
  • 46
  • 1
    looks like your problem is actually not related to qt, but you just cant call a function defined in main.cpp from a different class that doesnt know about that function – 463035818_is_not_an_ai Nov 17 '16 at 13:37
  • 1
    You are not only new to Qt, you are also new in C++... Reading a good C++ [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) will help you. You just have to declare function before using it: add in mainwindow.cpp, before `on_StartMotor_clicked` this line: `void SendCmd(INT Value);`. Or [create a header](http://stackoverflow.com/questions/20015656/how-to-create-my-own-header-file-in-c) (which is better) – Garf365 Nov 17 '16 at 13:38

1 Answers1

1

This is not an issue with QT but with basic C++. My general recommendation, therefore, is to buy some good book and learn this language beginning at the basics.

In your particular setting, you have two cpp files. In this context, they are called translation units because each of these files are compiled separately. The result of this are so-called object files (.obj). The linker has the job to make functions of one object file known to the other file.

Now, the linker can only do his job if the translation units know of the declarations of functions of other translation units.

Usually, you would have some header file that contains this declaration.

main.h:

void SendCmd(INT Value); // what type is INT by the way?

Now, both main.cpp and mainwindow.cpp should include this file. This way, you do not get a compiler error because mainwindow.cpp has a declaration of this function to call. And since main.cpp is compiled, the linker has some implementation of this function in his repository of functions and can let mainwindow.cpp know about it.

IceFire
  • 4,016
  • 2
  • 31
  • 51
  • Yes I will read a book :) but now I moved all my functions to a header file named "tmclfunctions.h". In my mainwindow.cpp and main.cpp I write the following: #include "tmclfunctions.h" and get an error saying my functions have been declared twice. – sci-guy Nov 17 '16 at 14:11
  • @Scientized I hope you have moved the function implementations to some cpp file that includes your header because this is the correct way to do this. You should also put include guards in your header file. In VS the code for this is `#pragma once` at the top of the file but there are also compiler-independent solutions. A quick google for "include guard" should help as it is basically three lines which does not fit well in this comment – IceFire Nov 17 '16 at 14:13
  • @Scientized You have to declare the function either static or inline if you define it in an header file and include it from more than 1 cpp files. So your function declaration should look like: 'inline void SendCmd(int Value);' or 'static void SendCmd(int Value)' – Shivam Jha Apr 19 '18 at 06:16