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

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.

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.

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

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.

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.

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.

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.

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.