-2

I'm currently working on my first GUI cpp application using QT. Right now my aim is to display a dialogue to the user with two buttons: Ok and Cancel. I've used signal and slot editor to make cancel close the window, however what I can't seem to find and manage to do is to run a function from another .cpp file when OK is being clicked. I've googled it and no answers seem to be relevant.

file "mydefault.cpp" -

void MyDefault::on_pushButton_clicked()
{
    \\ call function from another .cpp file
}

function which im trying to call in "server.cpp"

void calldefaults(){
...
...
...
}

Question - how to call function "calldefaults()" in "server.cpp" from "mydefaults.cpp"

Any clues or tips are more than welcome, if the answer is super dump just throw it at me :)

Thanks

Alan os
  • 29
  • 7
  • 1
    you mean [How to call on a function found on another file?](http://stackoverflow.com/questions/15891781/how-to-call-on-a-function-found-on-another-file) – saygins Nov 13 '16 at 21:49
  • 1
    Consult your book about declarations and definitions and header files. You can place function declaration in a header file and have it available if you include that header. By the way, there's `QDialog` that seems to be what you're trying to accomplish – krzaq Nov 13 '16 at 21:49

1 Answers1

1

This is important thing to understand in C / C++ (as opposed to Java, etc). Each .c/.cpp file is compiled separately, then linked together. When the mydefault.cpp is being compiled, it needs to know that the calldefaults() function exists. To do that, you need what's called a "prototype". At the beginning of the mydefault.cpp file, outside of any function declaration, put a line that looks like:

void calldefaults ();

and call the function like:

calldefaults();

That will work - if you have linking issues, ensure both files are included in your compiler command.

Mikes
  • 46
  • 6
  • 1
    For larger systems, the prototypes (first code) would go in a .h file that would be #include in each source file that would call the function. – Mikes Nov 16 '16 at 23:30