Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Including CPP and H file into form 4

Status
Not open for further replies.

biot023

Programmer
Nov 8, 2001
403
GB
Hallo - I have a cpp file and an h file called "polygon.cpp" and "polygon.h" respectively. The h file defines a number of classes, and the cpp file provides the implementation.
At present, I have '#include "polygon.h"' at the top of the cpp file, which seems fine.
However, I'm using the line '#include "polygon.cpp"' at the top of a form's cpp file (say "Form1.cpp") to include these, as this is the only way I can get things to work.
I'm sure this can't be right, as I've never seen an include command for a cpp file - can anyone tell me the correct way to do this?

Cheers,
Douglas JL Common sense is what tells you the world is flat.
 
Put your #include "polygon.cpp" in your .h file.
James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
So that polygon.h has '#include "polygon.cpp"' at the top, and the form's cpp file calls '#include "polygon.h"'? Common sense is what tells you the world is flat.
 
Actually, polygon.h should have the include statement at the end of the function declarations. What should happen is the form calls the .h file which declares the functions and calls the .cpp file which loads the functions.

James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
Ah - I see - cheers, man. Common sense is what tells you the world is flat.
 
the #include "polygon.cpp" won't hurt anything. I do it all the time. Mainly because I forget to put the #include at the end of the .h and am too lasy to go back. It won't hurt anything. Cyprus
 
Thanks - nice to know there's other lazy people out there. Common sense is what tells you the world is flat.
 
I've tried putting the #include"filename.cpp" at the bottom of my .h file, but I keep getting the following error:
E2090 Qualifier 'Tmy_class' is not a class or namespace name

This is always followed by:
E2040 Declaration terminated incorrectly

If I comment out all parts that might cause this error, it them moves on to the next class constructor's implementaion & reports the same errors there.

Anybody any idea what I'm doing wrong?

Cheers,
Douglas JL Common sense is what tells you the world is flat.
 
Ive done the include cpp in my form since I started
and I guess I did not know any other way. when you teach yourself you go with what works first. If it works dont fix it and all that. I would imagine your order of declarations or some such are out of line. Since you wrote the first h file anticipating the inclusion of the cpp in the form you migh have to restart the h file with the other intention. I sugest you finish your project and remember to try the other way on the next job. Im am going try it on my next project when the need arrises.
 
Sorry to lecture, but your programming style is like nails scratching on a blackboard for me.

You should never include .cpp files. It's not that it's lazy to do so... it's just plain wrong. You're supposed to compile .cpp files, not #include them. If you want to include function definitions as inline code, that's fine, just do it in a header file.


As for your error, I suspect it's because of your #including. When your compiler tries to compile the source code (.cpp) for Tmy_class, it can't find a declaration for the class. Your header file #includes the source code, but nothing tries to compile headers... they're only included. To remedy this, have your .cpp #include your .h file and stop the vice versa from happening.
 
To answer your original question:

The correct way of doing what you're trying to do would be to have polygon.cpp and Form1.cpp #include polygon.h. As long as you have everything declared in the header file, both source files should compile fine. They'll then be linked together. I'd assume Borland does that part for you.
 
Sorry if I'm being dim, but to clarify chipperMDW's point:
Do you mean all I have to do is #include the .h file, and the compiler will read any & all .cpp files in my folder & compile them fine into my project?
- & don't apologise for lecturing or criticising my programming style - I WANT to get it right & appreciate all input.
I'll retry with this project by just #including the .h file in the .cpp - should this then be accessible to my form?
Thanks,
Douglas JL Common sense is what tells you the world is flat.
 
The cpp file is not compiled in your project if you do not add it to the project file
It is not include with #include for performance reasons.
If polygon.cpp in included in your form then the form would recompile every time polygon.cpp is changed.
Right click the project.exe in cbuilder and choose "add" to add it to the project.
 
I'm afraid I'm still not getting the hand of this - I'll put down what I'm doing:
I have a form called mainform.cpp with all associated Builder files.
I have a .cpp file called polygon.cpp
I have an .h file called polygon.h
At the top of polygon.cpp is #include"polygon.h"
I then clicked Project|Add to project and added polygon.cpp to the project.
I then put (in mainform.cpp) #include"polygon.h"

This gives me a compiler logic error F1004.
I remove from mainform.cpp the line #include"polygon.h", and none of my polygon classes can be read.

I'm afraid the only way I can make things work at the moment is my calling #include on polygon.cpp

Can anybody see where I'm going wrong?

Cheers,
Douglas JL Common sense is what tells you the world is flat.
 
try to edit polygon.h and change prototypes to extern
 
I'm not sure what you mean, sorry - prototypes?
polygon.h is an .h file that I wrote all myself - no automatically generated bits.
Should I add anything to it to work? Common sense is what tells you the world is flat.
 
every function in the cpp file has a declaration in the header file.

a function

void foo( void ) {
// do something
}

has this line in the header file

void foo( void );

that's a prototype

extern void foo( void );

is a prototype to indicate that the function is included in a library or object file.
polygon.cpp will compile to polygon.obj

 
Ah, I see - would I do this for each class prototype, each class function prototype, or both? Common sense is what tells you the world is flat.
 
The functions to start with.
The linker will let you know with an "unresolved external" message what's missing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top