I have to admit, constructors have been one of the most challenging pieces of C++ for me to grasp so far - the reason being is that they are so critical to making all of the pieces work when you are trying to take the leap from placing all of the elements of a program in one file, to separating them into different files in true object oriented programming (OOP) fashion. The book I am using is so incredibly vague when explaining constructors, that I found myself piecing together this program based on what was on the book rather than building something because I really understood the concepts. I don't learn well unless I understand all of the underlying concepts, so when I stumbled upon the AntiRTFM tutorials on YouTube, I was delighted. What a great teacher, this fellow is! He really breaks it down and makes difficult topics digestible. Nevermind the imperfect English - he teaches better than many native English speakers I know.
Constructors were covered somewhere between 35-39, I believe, but this video (and a couple of earlier ones jumps into pointers in the memory. He also created this nifty visualization of how storage works behind the scenes. I already understood the storage/memory piece, but who doesn't like a good visualization! C++ Tutorial (44) - Absolute n00b spoonfeed
I thought that maybe my IDE (the tool I use to write code) was just buggy because I couldn't separate the files and get them to run correctly. I even downloaded a second free IDE for C++(CodeBlocks) to run my code to see if that was the problem. Nope. Same errors. Finally I walked through the above AntiRTFM videos carefully and watched for indicators to see what I was doing wrong. As it turns out, the header file must have a declaration of the constructor (with no 'type,' and matching parameters to the defined constructor in the cpp file, with a call to the header file (#include YourClass.h). The main cpp file should also include the header file, but the only thing needed to link the main file to call the constructor is to merely call the class as an instance - like calling an int variable, instead, you just type, for example, "YourClass xyz;." Finally, in the YourClass.cpp file, when defining the constructor, only use ONE constructor. For example:
YourClass::YourClass (int optionalVariable, int optionalVariable2 . . .)
{
Initialize private variables to a number or to one of the optional variables
};
And that is your definition. Remember, in the header file, the constructor parameters must match - for example:
class YourClass
{
public:
YourClass (int, int . . .);
type other functions here
private:
type your private variables here - you will initialize these in the constructor definition.
}
Now, I am using the free version of Visual C++ 2008 Express Edition. The AntiRTFM videos state that you should type #include "stdafx.h" at the beginning of all of your cpp files. If you are using Visual C++ 2008 Express, don't worry about doing this. I doesn't work. Apparently, the express edition doesn't allow for precompiled header directives (like stdafx.h) unless you dish out the 500 something dollars for the enterprise edition, as described in this forum. The files will still compile fine in the express edition, but are just a little slower. For me, when the compiler takes longer, I know I wrote a successful program. Otherwise it returns a lovely error report right away.
Maybe this will save someone the three hours of research I did to find this information. At any rate, I won't forget it!
Sunday, October 18, 2009
Visual Studio Express, Constructors and Pointers - #include "stdafx.h"
Labels:
#include "stdafx.h",
AntiRTFM,
constructors,
pointers,
visual studio
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment