C++ Workshop - Getting Started with C++ (Ch. 1 & 2)

Started by
182 comments, last by Dbproguy 15 years, 12 months ago
Quote:Original post by Myotis
When working on a big project, how will the final product be structured ? Will it be all in a single .ccp file like, a 100 meg text file ? Or can you create multiple programs, compile them in .obj and then link them all together ?

Thanks.


It will generally depend on the project. Most projects will be the easiest to arrange int several header (.h) and source (.cpp) files that will be compiled into a large exe. Some projects will warrant creating a separate library (either statically or dynamically linked) and use them with another exe.

Really it is whatever makes the most sense to you. You may work best with one cpp file with 100,000 lines of code, but I guarantee others will have a very difficult job following it.
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
Advertisement
Hello all,

My apologies if this has already been discussed, but I can't seem to find it anywhere...

I've been going back into some of my previous programs and want to 'break them down' into header files and other smaller .cpp files. But everytime I do this the compiler tells me it can't find the appropriate header files.

Since INCLUDE was discussed in chapter 2 I decided to post this here. Could somebody please give some information has to how exactly this is done using 2005 express? Thank you!

Palejo
The keyword is actually '#include', not 'INCLUDE'. Regardless; it's pretty simple. You '#include "filename.ext"'. This is different then standard headers which are usually included using the '#include <filename>' syntax. Now; the preprocessor has to be able to find the file. The path in the include statement is relative the file including it, so if 'C://code/foo.cpp' includes 'bar.hpp', the preprocessor will look for 'C://code/bar.hpp'.

Also, remember to add all your cpp files to your MSVC project so that the IDE knows to compile and link all of them. (And, well, add the headers too - but they don't technically get compiled; they just get included into a cpp file, which gets compiled.)
Glad to see things have picked up around here again:)

Ok, I'm getting closer here on combining multiple cpp files but could still use a little help.

I'm now getting errors such as the following:

character.obj : error LNK2005: "public: __thiscall character::character(int,int,int,int,int,int)" (??0character@@QAE@HHHHHH@Z) already defined in chapter6.obj

After some googling it seems I'm linking to the wrong run-time libraries...(http://www.gamedev.net/community/forums/topic.asp?topic_id=293675)

I've tried setting my libraries to multi-threaded dll but still get the same error:( Do I need to include another library in my pgm to get this thing to work? Or is there some other big concept here I'm still missing? Thanks again folks,

Pal
That means you included the definition of the function in more than one compilation unit. Each cpp file is a compilation unit. The headers you #include should contain only the declaration of the function, not the definition. When they include the definition, that code gets compiled into every compilation unit that includes the header. Then, the linker discovers multiple copies of it and, not knowing witch to pick, goes caput.

The difference between a declaration and a definition is simple.

void function(); //This is a declaration.void function()  //This is a definition. It has a code block.{   //code}

You stick declarations in the header file, then place the definition in one and only one cpp file.

//file my_header.h#ifndef MY_HEADER_H#define MY_HEADER_Hvoid function();#endif


//file my_header.cpp#include "my_header.h"void function(){   //code}


//some other .cpp file#include "my_header.h"void some_other_function(){   function(); //call the function declared in my_header.h}


And that just used up my 'nice answer' quota for the whole week.
Howdy!

I just wanted to say that I just received my book from eCampus, and I'm stoked to be able to get started with the workshop! This is something I've been really wanting (and needing), so it couldn't have happened at a better time.

Additionally, it coincides with my completing "Beginning C++ Game Programming" by Michael Dawson, which is a very readable book, but it just didn't have the depth that I was looking for. But I finished the last chapter today and I'm looking forward to this workshop, and hopefully there will be a collaborative atmosphere so that I and others can get the most out of it.

Thanks for putting this together!!!
evan k. stone | recombinant---------------------------------------aspiring game + audio programmersf bay area, ca, usa
Yes, I have read through both books and they are both excellent. However, Sams Teach Yourself C++ in 21 days goes into each subject more in depth. Dawson's book seems to rush through the subjects, and if you have further questions or would like further examples of a particular thing, you will be left wanting.

Dawson's book is similar to Sams Teach Yourself C++ in 24 hours, in that they both seem to be aimed more at teaching programmers of other languages C++ in a short amount of time then they are to teach new programmers not only how to use C++, but also knowledge about the concepts of programming in general.

I think Jeromy made an excellent selection on the book to use for this workshop. Just about any Sams Teach Yourself book is worth the time to read and study.
I thought the Dawson book had a nice flow to it, but it was a bit thin (it's only 324 pages), and it was more of a "Intro to C++ with Game-Related Examples". A good intro book though.

From what I've seen so far it looks the "...21 Days" book like a pretty decent text. It's certainly heavy enough (physically). Since I've been a professional software developer for the last 12 years (in other languages than C++), I pretty much fit into the category you describe - someone who's done programming in other contexts but would like to learn C++. So I'm really looking forward to this workgroup -- and in particular the interactivity of being able to discuss the chapters, exercises, etc. up here on the forums.

I think the project that Jeromy announced is going to be a fun one to work on as well. Really looking forward to tackling that one.

Thanks!
evan k. stone | recombinant---------------------------------------aspiring game + audio programmersf bay area, ca, usa
Pretty new to the forums, just orderd the book and hoping to join in soon :)
Welcome. =) We're a little over half way done, but feel free to reach the first 12 chapters and most your questions in the appropriate threads while you get caught up.

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints

This topic is closed to new replies.

Advertisement