开发者

new to C++, confused about separating code [closed]

开发者 https://www.devze.com 2023-01-15 02:58 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

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 1 char* and returns int
  • 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!

0

精彩评论

暂无评论...
验证码 换一张
取 消