Anusha Pachunuri
Anusha Pachunuri

Reputation: 1419

including header files in .cpp files

I have a file SpreadSheetCell.h and SpreadSheetCell.cpp.
I have another file SpreadSheet.cpp which includes SpreadSheetCell.h .In this case it is giving me errors like "undefined reference to SpreadSheetCell:SpreadSheetCell()" etc.
But when i try to include SpreadSheetCell.cpp instead the errors are gone. Is it not common to include the header files rather than the cpp files? What can i possibly be doing wrong?

Upvotes: 2

Views: 502

Answers (3)

Georg Fritzsche
Georg Fritzsche

Reputation: 98964

That is a linking error and means that you are not including a sourcefile that has the definition for SpreadSheetCell:SpreadSheetCell() when compiling.

You are probably missing SpreadSheetCell.cpp in the command line you use, e.g. something like:

g++ -o myBinary SpreadSheet.cpp SpreadSheetCell.cpp [...more files?]

Upvotes: 3

rogelius
rogelius

Reputation: 53

Ensure your SpreadSheetCell.cpp has a #include "SpreadSheetCell.h", if not show your code and your compiling commands!

Upvotes: 0

Michel Keijzers
Michel Keijzers

Reputation: 15347

It's never a good idea to include source code. Always only include header files.

I can't look in your file(s), however, I think that SPreadSheetCell.h includes a file (SpreadSheet.h maybe?) so that a circular dependency exist.

The best way is to make a graph of the files that are included (in both cpp and h files). If there is a 'circle' somewhere you have found the problem. In that case you should remove one link so the circle is broken or split files up in smaller files (also to remove the circle).

(with 'circle' I mean e.g. a file A including B including C including A again.)

Upvotes: 1

Related Questions