User defined Header

This is a discussion on User defined Header within the C++ Programming forums, part of the General Programming Boards category; Firstly, do not include .cpp files. Remember that as a rule. One exception... templates. Although I call those hpp's....

  1. #16
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Firstly, do not include .cpp files. Remember that as a rule.
    One exception... templates. Although I call those hpp's.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Well, templates are usually put in headers, yes?
    I always put them in headers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Quote Originally Posted by Elysia
    Well, templates are usually put in headers, yes?
    I am sure that you recall the trick of placing the implementation of the templates into a .cpp file, and then including that in the .h file.

    Of course, if you were feeling silly enough, you could use ".h" for source file extensions and ".cpp" for header file extensions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    I remember that trick, but I don't use it. All my templates go into headers.
    So, I never include .cpp files. Ever.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I find it useful to include a separate hpp for template source. IMO, putting it all in the header makes the header ugly and cumbersome and breaks the paradigm that all your other non- template classes follow.

    But I digress.....

  6. #21
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    thanks for all the replies.

    Also, you need some header guards in your .h file and get that using statement out of the header; you should never put a using statement in a header file.
    if I'm not wrong, I read that i have to use the using statement for I will be using it in loading the file .h.

    Code:
    #include <string>
    using std::string;
    
    class Name
    {
          public:
                 Name( string );
                 void setName( string );
                 string getName();
                 void displayName();
                 
          private:
          string name;
    };
    the one in blue uses the class string ( I'm not sure if the string is class) if I will not use the using std::string it will cause a problem on loading the .h from the main file.

    You don't need void in the parameter list in C++. It's unnecessary - it just takes space with no effect!
    thanks, next time I post a program you will never see void there in the parameter ^_^

    If this is too difficult for you, use an IDE such as Code::Blocks or Visual Studio. It will do this for you automatically.
    thanks for the advice, but I prefer to know how linking works. I kept researching about that but until now I can't find any resource on how to link those two .cpp and use the .h file ro your main program.

  7. #22
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You don't need to have a using declaration for using std::string. You can also explicitly qualify it.

    Therefore, replace all occurences of 'string' in the class definition (not only the blue one, but also those in the member functions) with 'std::string'.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Quote Originally Posted by $l4xklynx View Post
    thanks for the advice, but I prefer to know how linking works. I kept researching about that but until now I can't find any resource on how to link those two .cpp and use the .h file ro your main program.
    Alright, that's your decision.
    But usually, you just do like
    g++ file1.cpp file2.cpp [options]
    And it compiles and links all files together. One program, one file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #25
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by cpjust View Post
    wow, I never knew that, but I just copied the program from the book I read. I will not tell the name of the book to protect their integrity. But how come? they should know that. Anyway, I removed the #include <string> and using namespace.

    Code:
    #ifndef NAME_H
    #define NAME_H
    
    //#include <string>
    //using std::string;
    
    class Name
    {
          public:
                 Name();
                 Name( string );
                 void setName( string );
                 string getName();
                 void displayName();
                 
          private:
                  string name;
    };
    #endif

  11. #26
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Yes, they should know that. A lot of authors should also know things such as int main() instead of void main(), but I guess these days almost anyone can publish a book full of crap.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #27
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Quote Originally Posted by $l4xklynx View Post
    Code:
    #ifndef NAME_H
    #define NAME_H
    
    //#include <string>
    //using std::string;
    
    class Name
    {
          public:
                 Name();
                 Name( string );
                 void setName( string );
                 string getName();
                 void displayName();
                 
          private:
                  string name;
    };
    #endif
    http://apps.sourceforge.net/mediawik...arameter_names
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No, no, now your header has include order dependencies. It requires that someone includes <string> and imports string into the global namespace before it can be included without errors. That's just as bad, if not worse, as having using statements or declarations. The correct form is

    Code:
    #ifndef NAME_H
    #define NAME_H
    
    #include <string>
    
    class Name
    {
          public:
                 Name();
                 Name( std::string );
                 void setName( std::string );
                 std::string getName();
                 void displayName();
                 
          private:
                  std::string name;
    };
    #endif
    The advice about parameter names is good, too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #29
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    I was able to fix the problem. My mistake before was I didn't build a project, instead, I coded all of them in separate files one-by-one but not as a whole project.

    Having problem with this at the bottom:

    Not grouped as one project
    Name.h
    Name.cpp
    People.pp


    When I consolidated all of the files in one project the program compiled successfully.

    Grouped in one project
    +Name.h
    +Name.cpp
    +People.cpp

    Somehow, I'm happy to fix it but I didn't get the idea on how to link those two manually. So with the compiler I have I don't have any option to link those two files that I wanted to link manually right? even with visual c++?

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you were typing commands, you would just type "gcc -o Name.exe Name.cpp People.cpp" (giving all the files that need to be compiled on one line). Same for Visual Studio's cl, except I forget what the equivalent of -o is.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sum of User Defined Matrix Powers
    By QuaX in forum C Programming
    Replies: 1
    Last Post: 04-19-2009, 11:33 PM
  2. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 12:55 AM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 02:53 PM
  4. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 06:16 AM
  5. win32 and user defined classes
    By paulf in forum Windows Programming
    Replies: 4
    Last Post: 04-16-2002, 06:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21