Making a header file

Started by
6 comments, last by Oluseyi 19 years, 6 months ago
Hey, I'm making a header file for a class declaration. It's my first header file, named "MyString.h" (after the class declared in the file). Here's the entire code for the file: class MyString { public: MyString( ); //Blank constructer MyString(const Mystring& otherMystring) //Copy Constructer MyString(const char *string) //Constructer with constant string char reverse() //Reverse method void print() //Print Method }; Is there anything else I'll need to put in there so it will compile right and work well with the actual program? Thanks -TJ

-IVHumble Student

Advertisement
You should always have sentinals in your header file, to prevent compile errors from the class being declared multiple times.
You should have:
#ifndef __MYSTRING_H__#define __MYSTRING_H__class MyString{public:MyString( ); //Blank constructerMyString(const Mystring& otherMystring) //Copy ConstructerMyString(const char *string) //Constructer with constant stringchar reverse() //Reverse methodvoid print() //Print Method};#endif /* __MYSTRING_H__ */

Where __MYSTRING_H__ is a unique constant (I use the filename like that)

Apart from that, you'll need to put the data members in your class, so the compiler knows what type and size they and the class are.
Thanks. Just for educational purposes, what exactly do those sentinals do?

-IVHumble Student

Quote:Original post by TraderJack
Thanks. Just for educational purposes, what exactly do those sentinals do?
They prevent infinitely recursive inclusion of files - situations where header A includes another header than directly or indirectly includes itself. Without them the preprocessor could reach its inclusion depth limit and the compilation would fail.

One more thing. Every statement - declaration or otherwise - must end with a semicolon. Your method declarations are lacking, which would result in multiple parse errors in your actual file.
Quote:Original post by Oluseyi
Quote:Original post by TraderJack
Thanks. Just for educational purposes, what exactly do those sentinals do?
They prevent infinitely recursive inclusion of files - situations where header A includes another header than directly or indirectly includes itself. Without them the preprocessor could reach its inclusion depth limit and the compilation would fail.
Also, if header A declares a class, and header B includes header A, then in a source file, if you include A and B, you'll get compile errors because the class is declared twice (as far as the compiler knows). It doesn't care that both declarations are exactly the same.
Quote:Original post by Evil Steve
Also, if header A declares a class, and header B includes header A, then in a source file, if you include A and B, you'll get compile errors because the class is declared twice (as far as the compiler knows). It doesn't care that both declarations are exactly the same.
False. You can have infinite declarations (otherwise forward declarations would not be possible). Multiple definitions, however, result in compiler errors.
Quote:Original post by Oluseyi
Quote:Original post by Evil Steve
Also, if header A declares a class, and header B includes header A, then in a source file, if you include A and B, you'll get compile errors because the class is declared twice (as far as the compiler knows). It doesn't care that both declarations are exactly the same.
False. You can have infinite declarations (otherwise forward declarations would not be possible). Multiple definitions, however, result in compiler errors.
Ah, I meant definitions. Its 4am. Pitty me :P
Quote:Original post by Evil Steve
Ah, I meant definitions. Its 4am. Pitty me :P
Yeah, heh.

You should never have definitions in a header, though, except for templates.

This topic is closed to new replies.

Advertisement