LNK2005 Error in genetic algorithm program

Hi, I have been doing a course on programming. We are currently looking at genetic algorithms. I wrote some code a year ago creating a simple genetic algorithm. I know the code was working fine and I hadn't touched it since.

I recently went back to the genetic algorithm recently to review how I did it originally and just to have a refresher into how I went around creating the program. However this time I went into the program it was showing up a bunch of LNK2005 errors.

I looked this up and people were saying that that error is if the variables are duplicated within the project (please correct me if I'm wrong) Or it happens if the .h file contains functions but the .h file is referenced more than once.

​

If I was going to move the functions for the objects to a cpp file would I just reference them like any other function or because I am using objects can I just declare the function like normal then call it when needed.

If you can review that would be awesome. here is the the github: https://github.com/Sirtankyou/Genetic-Algorithm.

If not does the error LNK2005 only show up when you do bad programming practices rather than bad code or is it both?

Thank you for your time.

4 comments · 100% upvoted · other discussions

comments

teraflop

In general, any time you want help with an error, you should post the complete, exact error message. That makes it much easier for people to help you.

Yes, you have function definitions in your header files, so when you include the same header in multiple source files, the function has multiple definitions, which isn't allowed. Your header files should contain only declarations of top-level functions; the definitions should go in a .cpp file.

(The exception is if you declare the function as inline, in which case you're allowed to have multiple definitions, because you're encouraging the compiler to make a copy of the function's code at every call site. And member functions declared within the definition of a class are automatically inline, so they don't cause problems in header files.)

Stretchslash [OP]

Okay thank you for the quick response. Ill make sure next time to post the complete and exact error message next time.

I see that However I looked through my code. I now understand that I didn't follow good practises when I put my code in my h files.

However I did look through my code and I it seemed to me that I only referenced each h file once. But ill just first put the functions in the correct file before I crate more work for you helpful people XD.

teraflop

You're welcome!

Individual.h is included twice: once in Console Genetic Algorithm.cpp, and once in Population.cpp.

If you were only going to reference a header file once, there wouldn't really be any point in making it a separate file in the first place! The whole point is to provide somewhere to put declarations that are used in multiple places.

procrastinatingcoder

Header guards