Implicit declaration of function with header file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 19
Reputation: Suraine is an unknown quantity at this point 
Solved Threads: 0
Suraine Suraine is offline Offline
Newbie Poster

Implicit declaration of function with header file

 
0
  #1
Apr 6th, 2008
Dear c++ gurus,

I'm new in creating a header file. never try before.

ERm... i have write a header file with a program:

  1.  
  2. Header file: MySensor.H
  3.  
  4. extern short int DetectSensor(int sensorvalue[10])
  5. {
  6. ....;
  7. }

  1.  
  2. Program file: MyFile.C
  3.  
  4. #include <MySensor.H>
  5.  
  6. short int i;
  7. int p, sensorvalue[10];
  8.  
  9. int getsensorvalue()
  10. {
  11. ...;
  12. }
  13.  
  14. int main()
  15. {
  16. for( p = 0; p < 10; p++)
  17. {
  18. sensorvalue[p] = getsensorvalue();;
  19. }
  20.  
  21. i = DetectSensor(sensorvalue);
  22. return 0;
  23. }


But it comes out saying that:
Implicit declaration of function 'short int DetectSensor(...)'

What happen? is it relate to my header file format is wrong?


Thanks.


Best regards,
Suraine
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Implicit declaration of function with header file

 
0
  #2
Apr 6th, 2008
1. The header file should not have any code -- just function prototype
extern short int DetectSensor(int sensorvalue[10]); Notice the semicolon at the end and there are no ( and )


2. In the *.cpp file, enclose the name of your header file in quotes
#include "MySensor.H"; Angle bracks are for header files that are were supplied by your compiler, such as <string>. The ones you create are in your project's folder and should be surrounded by quotes.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: Suraine is an unknown quantity at this point 
Solved Threads: 0
Suraine Suraine is offline Offline
Newbie Poster

Re: Implicit declaration of function with header file

 
0
  #3
Apr 6th, 2008
Thanks, Ancient Dragon.

So, that means in header file. We can just declare the function prototype, but cant have its function definition.

only:

  1. extern short int DetectSensor(int sensorvalue[10]);

but not

  1. extern short int DetectSensor(int sensorvalue[10]);
  2.  
  3. short int DetectSensor(int sensorvalue[10])
  4. {
  5. function definition block;
  6. }

function definition should be done in *.cpp file, right?

If it is, what is header file for? not for shorten the file length, but for?

Thanks again.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Implicit declaration of function with header file

 
0
  #4
Apr 6th, 2008
Header files for mostly useful in multi-file projects where there are two or more *.cpp files which need to know about the same functions and definitions. Look in your compiler's include directory, it contains many header files. The ones you write are no different than those.

The more you write programs and header files the more useful they will appear to you. Especially when you begin to write multi-file programs. Many large projects consist of hundreds of *.cpp files, so common header files are absolutely essential.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC