Thread: User defined Header

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

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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,895
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,895
    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,336
    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.

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, 03:53 PM
  4. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM
  5. win32 and user defined classes
    By paulf in forum Windows Programming
    Replies: 4
    Last Post: 04-16-2002, 06:12 PM