Scope question (or linking? multiple files)

Hi I was curious about something. In following my book exercises my compiler is throwing all sorts of errors @ me (codeblocks 10.05) so I decided to do things a different way. I will give the example from my book and then the way i've managed to get the code to work. My question is whether or not my code is "hack" and if it should be avoided, or if it is a perfectly normal way of implementing what I'm trying to do.

For example the book has me create 3 different files

main.cpp / golf.cpp / golf.h

and to execute the program

main.cpp include <iostream> & "golf.h"
golf.cpp include <iostream> & "golf.h"

Upon execution of the program the compiler throws me const variable errors, variables pre defined elsewhere, abnormal references and all that jazz(i made up half these terms i think)



What I have done..

main.cpp include <iostream> & "golf.cpp"
golf.cpp includes "golf.h"

Now everything works flawlessly. The thing which lead me to believe this was the appropriate way to type the code was that i could see main.cpp and golf.h were linked and golf.cpp and golf.h were linked, but there was no link between main.cpp and golf.cpp.



Is this poor linking on my part? Is it bad practice? I'm new to this multiple file thing and don't want to ingrain things that shouldn't be in my mind.




Last edited on
In normal situation, your hack is bad practice! Especially a .cpp file is included in another .cpp file.

We normally put declarations and class definitions in .h files, put definitions and implementations in .cpp files, include necessary .h files from .cpp files.

You need to fix the errors of the book's example.
Shoot me now, i've solved it (;

@EricDu thank you for your insight!
Last edited on
Topic archived. No new replies allowed.