Sign in to follow this  
TraderJack

Making a header file

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 constructer
MyString(const Mystring& otherMystring) //Copy Constructer
MyString(const char *string) //Constructer with constant string

char reverse() //Reverse method
void 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this