PDA

View Full Version : Using the same function in multiple files (QT/C++)



Zorgoth
January 19th, 2009, 01:42 AM
I'm using QT with C++ and a while ago I put some functions in a header to use in a program I'm writing. But now that I have two separate .cpp programs using it, I'm getting errors from "make" about multiple definitions of functions from said header file, one in each .o file including my header.

The functions are defined on the same line of the same file, with the usual #ifndef #define #endif routine to protect the definitions.

It should be noted that this is the first time I've done a project involving multiple .cpp files.

Is there something special one has to do to use a header file in C++/QT for anything but a class definition?

Zorgoth
January 19th, 2009, 01:51 AM
Incidentally, why can't I mark threads as solved anymore? Did they move it at some point?

SledgeHammer_999
January 19th, 2009, 01:55 AM
Here's an example:
test1.h


//declaring prototype of "my_function" function
int my_function(int a);


test1.cpp


#include "test1.h"

//actually implementing "my_function"
int my_function(int a)
{
return a+1;
}

//calling "my_function"
int main()
{
int b;
b = my_function(4);
}


test2.cpp


//just calling "my_function" from another function
void foo()
{
int b;
b = my_function(5);
}


Hope you understand how to use the same function.
Just remember: You need to implement it just ONCE.

Zorgoth
January 19th, 2009, 02:19 AM
Thanks! I've got it working. I think I understand these better now; declaring the function with the semicolon in the header file was the key.

snova
January 19th, 2009, 02:29 AM
Incidentally, why can't I mark threads as solved anymore? Did they move it at some point?

Temporarily disabled. See: http://ubuntuforums.org/showthread.php?t=1039481

It's interesting to note how many people have been missing it as of late...