Dear c++ gurus,

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

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

Header file: MySensor.H

extern short int DetectSensor(int sensorvalue[10])
{
      ....;
}
Program file: MyFile.C

#include <MySensor.H>

short int i;
int p, sensorvalue[10];

int getsensorvalue()
{
       ...;
}

int main()
{
      for( p = 0; p < 10; p++)
      {
            sensorvalue[p] = getsensorvalue();;
      }

      i = DetectSensor(sensorvalue);
      return 0;
}

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

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.

Thanks, Ancient Dragon.

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

only:

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

but not

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

short int DetectSensor(int sensorvalue[10])
{
    function definition block;
}

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.