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.
 
That's brilliant, thanks - very appreciated.
Cheers,
Douglas JL Common sense is what tells you the world is flat.
 
Here's a short and sweet example of what you want your files to look like:

[tt]// polygon.h
#ifndef POLYGON_H
#define POLYGON_H

class Polygon
{
public:
void draw() const;
};

#endif


// polygon.cpp
#include "polygon.h"

void Polygon::draw() const
{
// drawing instructions
}


// main.cpp
#include "polygon.h"

int main()
{
Polygon poly;
poly.draw();

return 0;
}
[/tt]

Make sure both your .cpp files are in your project. The idea is that you need the declarations for your classes and functions (in the header file) to be visible (via the #includes), but the definitions aren't required to be there to compile the code. In other words, you don't have to know what a function does to be able to use it, you just have to know that it exists.

After you compile all those source files, they'll be .obj files, which Borland will link together for you as part of the build process. Here's where all the definitions get linked in. If anything's missing in this step, you'll get an unresolved external as mentioned above. (I don't think this is the source of your problem, since functions are declared external by default.)
 
butthead, the reason it compiles and works correctly is that the code in the #included .cpp files is being treated as inline code. It's not being compiled as a .cpp file, its text is just being included as if it were a .h file.

Making it a .cpp file implies that it is meant to serve as the main part of a translation unit. That means it's meant to be compiled. When you use it otherwise, the .cpp extension is misleading and wrong. If someone were to actually compile the #included .cpp file and link it into the project (which would, again, be the intended use of .cpp files), you'd likely get duplicate definitions or, at the very least, generate too much code. You'd have some inline code generated from the .cpp when it was included and you'd have the exact same code compiled and ready to link. That wouldn't be very efficient, and it would probably end up causing subtle errors in the long run.

Also, as hennep said, by including the .cpp file, you introduce unneeded dependencies. When you #include it, any time you change it, you'll have to recompile any file that #includes it. That's not such a big deal when your entire project takes 5 seconds to compile. When it takes several days, you're causing a big problem. By treating it as a .cpp file should be treated and compiling it, you have fewer dependencies and, if you change that source file, you only have to recompile it and relink your project. That takes considerably less time that the combined compile/link process.

In short, just because it works doesn't make it right. A program that calls new and never calls delete still works, but it's got a resource leak. The setence "I am the queen of France" is a proper statement, but it's not true. You can use a screwdriver to put a nail into a board, but... why?
 
Explains alot of the duplicate errors I get sometimes.
I Just start juggling until everthing works. And sometimes I just except less than perfect. I havent had the training that some of you have obviously had. I started coding as a hobby and have fallen in love with it.
I do appreciate your patience.
 
because maybe all you have is a screwdriver and the nail has to be in the board now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top