User defined Header

This is a discussion on User defined Header within the C++ Programming forums, part of the General Programming Boards category; hi, here I am again. I'm in the pace where you will code a header or header of your own ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    115

    User defined Header

    hi, here I am again. I'm in the pace where you will code a header or header of your own in a different file for program reusability. I have tried the code many times in different ways. I decided to make it small as shown below so that I could easily check the error quickly, unfortunately, I cannot see any problem in my code. I have no idea what's happening, but I've searched over the net and they have the same structure as mine. I don't know if it's with the compiler, but if I created Name.h without seperating the implementation of that class it works. I hope you could help me with this and give some advice. Hope to hear from you soon.

    file: Name.h
    Code:
    #include <string>
    using std::string;
    
    class Name
    {
          public:
                 Name( string );
                 void setName( string );
                 string getName();
                 void displayName();
                 
          private:
                  string name;
    };


    file: Name.cpp
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    #include "Name.h"
    
    Name::Name( string n )
    {
       setName( n );
    }
    void Name::setName( string n )
    {
       name = n;
    }
    string Name::getName()
    {
       return name;
    }
    void Name::displayName()
    {
         cout << "Name: " << getName() << endl;
    }

    file: Person.cpp
    Code:
    #include <iostream>
    #include <string>
    using namespace::std;
    
    #include "Name.h"
    
    int main( void )
    {
        Name emp1( "John Travolta" );
        Name emp2( "Nicolas Cage" );
        
        cin.get();
        return 0;
    }

    Error:
    C:\DOCUME~1\INFORM~1\LOCALS~1\Temp\ccYbcaaa.o(.tex t+0x1ac) In function `main':
    [Linker error] undefined reference to `Name::Name(std::string)'
    [Linker error] undefined reference to `Name::Name(std::string)'
    C:\DOCUME~1\INFORM~1\LOCALS~1\Temp\ccYbcaaa.o(.tex t+0x1ac) ld returned 1 exit status

    Compiler:
    DevC++ 4.9.9.2

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You are compiling as a project, yes? Each file compiles separately and then gets linked together by the linker -- if you didn't compile Name.cpp into Name.o, it can't get linked in.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    thanks for replying. um, I'm not creating a project. I'm reading a book and reached that topic about program reusability. I compiled name.cpp, I got this result from the compiler.

    Error:
    [Linker error] undefined reference to `WinMain@16'
    ld returned 1 exit status

  4. #4
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    You haven't defined main(). Every C program needs an entry function called main, and it must return int.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    You haven't defined main(). Every C program needs an entry function called main, and it must return int.
    hi, thanks for replying. I have a main function in person.cpp. I don't need a main function name.cpp because that's not the program that should have the main function.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by $l4xklynx View Post
    thanks for replying. um, I'm not creating a project.
    Two source files == project.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by tabstop View Post
    Two source files == project.

    thanks, but I wasn't able to fix the problem yet.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    You need to compile both .cpp files, and link them. No separately.
    Or you can use an IDE and put them into a project, which will make the IDE compile all the files.
    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. #9
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    you failed to include Name.o (or whatever it is called) in the filelist to be used by the linker...

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    I Tried to compile the Name.cpp
    I got this error message:
    Code:
      [Linker error] undefined reference to `WinMain@16' 
      ld returned 1 exit status
    so I guess I cannot link both of them if there's an error message. I tried to put a main in the Name.cpp file, it was successfull but the Person.cpp doesn't it load it successfully still. I've seen lots of programs that is creating their own .h file to separate classes so I hope I could do that as well.


    I've tried the code at the bottom and it worked, wierd. When I load the ( #include "Name.cpp" ) does it load the ( #include "Name.h" ) that was loaded by that .cpp file?.


    Code:
    #include <iostream>
    using namespace::std;
    
    #include "Name.cpp"
    
    int main( void )
    {
        Name emp1( "John Travolta" );
        Name emp2( "Nicolas Cage" );
        
        emp1.displayName();
        emp2.displayName();
        
        cin.get();
        return 0;
    }
    I wasn't aware of the linking process. I'm using DevC++ 4.9.9.2. I think I have to research about that. Is that something that I will hard code? or something that will do in my compiler for the compiler to link the two files?

    sorry for the lack of knowledge about this, I'm just self studying. I have no private teacher or in a school learning this, so it's kinda hard. But I'm willing to learn so everyday I have nothing to do at home but to exhaust myself in programming and trying to understand all things on my own.
    Last edited by $l4xklynx; 11-27-2008 at 03:04 AM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    Firstly, do not include .cpp files. Remember that as a rule.
    Secondly, there are two stages to compiling:
    - Compiling.
    - Linking.
    First, you must compile both files.
    Then you must link both files.
    Do not do these separately. Do not compile & link each file separately. It won't work.

    If this is too difficult for you, use an IDE such as Code::Blocks or Visual Studio. It will do this for you automatically.
    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.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you are using command line compilation, then you need to do something like this:
    Code:
    gcc -c name.cpp
    gcc -c person.cpp
    gcc -o person person.o name.o
    The -c switch tells the compiler that the source file you supplied is NOT the complete source for the application, and thus it should not try to link... The last line takes the object files produced by the compile steps and links them together, along with the C runtime library.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you're getting an error about WinMain then you aren't compiling it as a Console App, you're compiling it as a Windows App. You need to change that, but I've never used DevC++ so I can't help you with that.

    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.
    "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

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    If you're getting an error about WinMain then you aren't compiling it as a Console App, you're compiling it as a Windows App. You need to change that, but I've never used DevC++ so I can't help you with that.

    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.
    No, gcc looks for EITHER main or winmain, no matter what - it doesn't have a setting for the linker to translate from one to the other, so it accepts both.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    Oh, and
    int main( void )

    You don't need void in the parameter list in C++. It's unnecessary - it just takes space with no effect!
    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.

Page 1 of 3 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-20-2009, 12:33 AM
  2. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 01: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, 07:12 PM

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