Where do I declare indentifiers?

CaptainMidnight

Warlord
Joined
Apr 16, 2006
Messages
141
I've just started using of VB 2003 standard edition. I compiled Bhruhic 3.13 BTS patch source code and it came out working normally (but larger in size). However, I'm rapidly making very little progress with my "own" code. I've been trying to graft the "UpgradeCost" building tag from the Warlords CCCP source code onto Bhruhics BTS code. What I find is that some identifiers are apparently undeclared. Don't you declare them in the header (.h) files? I can't see where I haven't done the declaration.

Any help for this poor, simple beginner would be greatly appreciated.
 
It depends on which identifies you are talking about and their scope, how familiar with C++ are you? (before I get into a overly-complicated technical explanation) :)
 
Ok, let's try the short definition :)

An identifier is normally something like pPlayer or iCount, basically a named variable. Declaring an identifier is the first step you have to complete before you can use it. So the following code will give you an error (undeclared identifier):
Code:
int myFunction(int x)
{
   y = 4;

   return x + y;
}
You get there error here because y hasn't been properly declared. Notice that x was declared as an argument (int x) for the function. In other words, the compiler knows that x is an integer but it doesn't know what y is. However, the following code would work:
Code:
int myFunction(int x)
{
   int y = 4;

   return x + y;
}
Now the compiler knows that y is also an integer so everything is fine. :)

Hope that helps, it's a bit simplified but it should get you pointed in the right direction.
 
Oh, I see... And lets say the part "int y = 4", the "4" could be a string or word that represents a variable or number from another equation.

Looking through CCCP code I often see values (variables? or simply words?) that don't seem to be declared in the sense you discussed above. This is what I think is happening crystalised in this wonderful example:

"myfunction" declares that int X is going to be use; in order to use there is another function that uses (and declares) both int y and int z, this function culminates in int x value. Therefore "myfunction" would not have to declare int y and z because they are already declared under int x.

So C++ is effectively a (3D) web of code, rather than just a linear list like XML.

(Pardon my naivety and cruel butchering of programming vocab)

Am I getting the wrong end of the stick?

I am working through a C++ tutorial, so heres hoping things will become clearer sooner or later. Thanks for your input Seven05
 
Not really, every variable will be declared somewhere within the function unless it is a member variable of the class that contains the method (proper name for a function in a class) or a global. However, variables can have scope, that is they can apply to only part of the method or function but not the rest, this is frequently seen in for loops where you have something like for (int i = 0, i < 10, ++i) in which case using 'i' outside of the for loop will return an undeclared identifier error in the compiler. Oh, and they have some macros in the SDK too, just to keep us on our toes and confuse the non-programmers out there even more.

What C++ is, rather than a web of code, is an object oriented language. So you will deal with a lot of objects in the form of classes. It is still very structured and limited by scope but until you have a basic understanding of object oriented programming (OOP) it can be quite confusing :)

Some good tutorial subjects to learn about would be OOP in general, classes (incl. private and public data) and of course the basics like variables and loops. The good news is that once you get the basics the rest falls into place pretty easily.
 
Top Bottom