Include <cstdlib> in header file but not main file? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Include <cstdlib> in header file but not main file?

Hi all, I was practicing class earlier and I was stuck in compiling the program error. My program has a cstdlib function which is abs, but i include <cstdlib> in my main.cpp first and it shows error. It works when I put include <cstdlib> in the header file. Could someone explain this? Thanks!

23rd May 2019, 1:20 AM
Cathy Liao
Cathy Liao - avatar
2 Answers
+ 3
If you expicitly use your function inside of "main" including cstdlib in main file should work. BUT if you create instance of your class in main, and then you call some method in which said "abs" is used, then you need to include library in this class file because linker will not know where to take function from during compilation.
23rd May 2019, 1:46 AM
Jakub Stasiak
Jakub Stasiak - avatar
+ 1
Exactly Jakub Stasiak ! As a general rule include it where you use it.
23rd May 2019, 7:32 AM
AZTECCO
AZTECCO - avatar