Getting slightly hung up on my first home work assignment for my Advanced C++ class

So… I just started my advance C++ class at my local community college.
The book being used for the class is Data Structures with C++ using STL by William Ford  and Willam Top.

The book isn’t what I call an easy read and if this was something I had no prior exposure to I think I’d be in a panic.  But, I’ve paged through the chapters and with most  code seems understandable.    I’m really looking forward to this class since I think it will fill in the estoteric gaps in my C++ knowledge.

Anyway, I start getting hung up on the very first assignment… ;(Because of family scheduling conflicts, I’m have to bug out of the class when the lab portion starts so I was on my own figuring this out instead of asking the instructor.)

So the assignment was Chapter 1, problem 21.  The Hilow game.   Easy I thought, compared to the Hell that I’ve gone looking at constraints in Heekscad this is nothing.

I thought, ok… a project with a  main.cpp,  hilow.h, hilow.cpp and d_random.h.    Everything looks right except I get this weird LNK2005 linker error in visual studio express…  Oddly enough, I run into a lot of errors, but this one was one that I haven’t run across yet.
A little googling takes me to here ( )
Hey… at least I’m not alone in the universe.
Basically I got taken to this link ( which had a really nice explanation as to what was causing my pain as well as some usefull trips on how (as well as why) to create re-usable code.

The long and short of it, I think the root cause of my problem was that implementation code was rolled into the header file, which caused me a bunch of problems when I tried link the code because of multiple definitions.   In order to get around this problem, I basically got rid of my main.cpp file and rolled my main() call into hilo.cpp and low and behold problems went away.

I wonders if the authors of the book constructed their code so it sort of limited its use, since it forces you to keep your main() and what your working on in the source file.

Anyway..  I need to put the finishing touches on this assignment.

This entry was posted in C++. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *