include

Hello, I have four files:
AllOnesGA.cpp, GeneticAlgorithm.h, Individual.h, Population.h
My idea is to include the .h files in the .cpp file but it doen't work. I have compilation problems.

So the solution that I have found is:
AllOnesGA.cpp
#include "GeneticAlgorithm.h"

GeneticAlgorithm.h
#include "Population.h"

Individual.h
nothing

Population.h
#include "Individual.h"

Is this correct? Or must I include something in the main code as for example:
Population::Population and so

Thanks
I cannot really see the problem here. If the header files cannot be found, you need to add the header files' path to your compiler's search directories.
Last edited on
It depends entirely on what the dependencies are.

Any file (be it .cpp or .h) should include the MINIMUM necessary includes to make it syntactically valid.

So if Population.h specifically needs something in Individual.h, then it should include it.
Anything including Population.h shouldn't have to second guess that Individual.h is also needed.


> you can include them all headers in one header, then include that header in your cpp file
This is generally a bad idea when your projects get larger.
1. It slows compilations down by everything having to drag in kitchensink.h
2. It breaks down your interfaces, because things which should fail to compile suddenly compile.
3. Pulling apart the resulting pile of spaghetti several years after someone thought kitchensink.h was a really good idea is a real PITA.
Topic archived. No new replies allowed.