I am new to C++, and I'm having trouble segmenting my code. Here is my setu开发者_如何学JAVAp:
- A main.cpp that calls function that takes 2
int
and 1char*
and returnsint
- A function.h that contains, among other things, a function prototpye
int function(int a, int b, char* c);
- A function.cpp that contains the actual function definition.
I have used #include "function.h"
in both function.cpp and main.cpp.
When I try to compile my code, I get that there is an undefined reference to function(int, int, char*)
in my main()
function.
What's wrong?
I guess that you haven't "protected" your .h files from being included multiple times.
Always construct your .h files with this preprocessor lines:
#ifndef __MYHEADERFILEH__
#define __MYHEADERFILEH__
your header file "code" here
#endif
______MYHEADERFILEH____ Is a uniqe name, preferable the header file's name in uppercase. You put a #include "MyHeaderFile.h" In each .cpp or .c file using the header file.
If you post your code it will be easier to pinpoint the probem.
*smacks head.
I left off a .cpp after the function file, so eclipse wasn't compiling that file. sorry!
精彩评论