Originally Posted by
silur
Simple question i have this code in an EXE:
void foo(int var1);
void foo(int var1,int var2);
void foo(int var1);
void foo(int var1,int var2);
To copy to clipboard, switch view to plain text mode
so i can use both with the same name
how can i use this in a DLL my Qt compiler says that the 2 function confilts
Firstly, there is no such beast as a Qt compiler. Your are using either a C++ or C compiler and this question has nothing to do with Qt.
Now, to address the C++ question. By way of example, this code:
#include <stdio.h>
void foo(int a) { printf("A = %d\n", a); }
void foo(int a, int b) { printf("A = %d B = %d\n", a, b); }
int main(int argc, char **argv) {
foo(1);
foo(2,3);
return 0;
}
#include <stdio.h>
void foo(int a) { printf("A = %d\n", a); }
void foo(int a, int b) { printf("A = %d B = %d\n", a, b); }
int main(int argc, char **argv) {
foo(1);
foo(2,3);
return 0;
}
To copy to clipboard, switch view to plain text mode
Is perfectly valid C++, which allows overloaded functions, and compiles just fine with a C++ compiler. Despite deliberately using only C IO functions this code will not compile with a C compiler (GCC in my case), generating:
gcc -o test main.c
main.c:7:6: error: conflicting types for ‘foo’
main.c:3:6: note: previous definition of ‘foo’ was here
main.c: In function ‘main’:
main.c:15:5: error: too few arguments to function ‘foo’
main.c:7:6: note: declared here
gcc -o test main.c
main.c:7:6: error: conflicting types for ‘foo’
main.c:3:6: note: previous definition of ‘foo’ was here
main.c: In function ‘main’:
main.c:15:5: error: too few arguments to function ‘foo’
main.c:7:6: note: declared here
To copy to clipboard, switch view to plain text mode
You are most likely using C++ compiler, but this is still related.
Even with a C++ compiler, if you have forward declarations in the include file for your library like this:
#ifdef __cplusplus
extern "C" {
#endif
void foo(int var1);
void foo(int var1,int var2);
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
extern "C" {
#endif
void foo(int var1);
void foo(int var1,int var2);
#ifdef __cplusplus
}
#endif
To copy to clipboard, switch view to plain text mode
then you are telling the C++ compiler these are C-style declarations and it will not allow the overloaded names (just like a C compiler):
g++ -o test test.cpp main.cpp
In file included from main.cpp:1:0:
test.h:6:27: error: declaration of C function ‘void foo(int, int)’ conflicts with
test.h:5:6: error: previous declaration ‘void foo(int)’ here
main.cpp: In function ‘int main(int, char**)’:
main.cpp:6:10: error: too few arguments to function ‘void foo(int, int)’
test.h:6:6: note: declared here
g++ -o test test.cpp main.cpp
In file included from main.cpp:1:0:
test.h:6:27: error: declaration of C function ‘void foo(int, int)’ conflicts with
test.h:5:6: error: previous declaration ‘void foo(int)’ here
main.cpp: In function ‘int main(int, char**)’:
main.cpp:6:10: error: too few arguments to function ‘void foo(int, int)’
test.h:6:6: note: declared here
To copy to clipboard, switch view to plain text mode
Exactly what you are doing we cannot tell, but this information should give you a clue.
Bookmarks