Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / simple syntax error

Author
Message
Super Mecha Cow
16
Years of Service
User Offline
Joined: 7th May 2008
Location:
Posted: 8th May 2008 01:00
Hey, another c++ newbie here with a smack-yourself-in-the-head easy question that I don't have the answer to.

I have a 2D float array. Everytime in my code that I refer to values from it, I use a constant int as the index as you can see in the code I have so far:



I get about 70 errors when compiling, most of which look like this:


I don't know if I'm mixing ints and floats wrong, or you just can't use variables to index arrays, or what, but does anyone know what I'm doing wrong?

Also, can you use an array value for db commands (such as putting an array value in the ID parameter)? Does it have to be an int or does it convert automatically.

Last question, what's the easiest way to convert ints to floats and back, etc?

Thank you for any help you can give me.
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 8th May 2008 08:15
I'm not seeing what the compiler is complaining about. It's saying that you're not closing off a pair of square brackets but it all looks okay to me.

C/C++ used to be fairly forgiving WRT promoting data types but I think it's tightened up on it since. There may be a problem with trying to place the value 100 in a slot designated for floats. I do question why you're using an int when the data clearly requires a float, but it may be some trick you're trying to pull off. That said....

First of all, using #define to force an expression to take on a value is frowned on. Instead of ......

Akkkkkkk!!! I just realized what your problem is. You never follow a #define with a semi-colon unless it needs to be part of your replacement text. What's happening is that everywhere you use

NPC_index

it's actually substituting

100; //The GDK ID. First number of NPCS


It's not only inserting the semi-colon, it's including the trailing comment. #define is a text substitution pre-process directive. It replaces the first bit of text with the whole of whatever follows, unless it's cobbled up to look like a function.

Back to what I was saying, using #define to replace constants is frowned on. Instead you should use

const int NPC_index = 100; // comment your heart out

Second of all, if you're going to insist on putting an int where a float should be, it's best to let the compiler know what your intent is by casting the int to a float by using something like

(float) NPC_index

And, finally, no you can't substitute a float for an image number but you can cast a float to an int in the expression with something like

(int) someFloatNumber

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
jinzai
18
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 8th May 2008 10:18 Edited at: 8th May 2008 10:44
#define is a little more than simple text substitution, that's what the bit that looks like a function is all about. It allows you to pass data into the text substitution, changing the outputted text. It is a MACRO in that sense. There are already a large number of predefined MACROS that you get from including "windows.h". MAKEINTRESOURCE is an example.

Using #define for known values is not necessarily frowned on. The frowning is based on misuse (like what happened here), as far as I can see. Using const is more of a data hiding technique where a static variable that is unchanging is necessary. The only decent example of that that comes to mind is the lines per page variable in MFC documents that are being printed. The variable is static, but not const in that case, however. It is necessary in that instance because the value must be accessed by Windows, too.

For cases like PI or RADIANS_PER_DEGREE it is best (imo) to use a #define. In that case, it is simply taking the place of a literal value that I could have just as easily typed in directly. If you have a const double radians_per_degree = 0.0174533, and a #define RADIANS_PER_DEGREE 0.0174533, the #define works faster, since it is an immediate value.

Anyway, the difference is ultimately that a #define utilizes compiler resources, whereas const utilizes runtime resources (memory, and time).

Your array is not what you intended at all, I think. It has only one element in the first dimension, which means it can only hold one NPC. There are more things about it, but I think I will just show you something, if you don't mind:

Start off by thinking only about 1 NPC. What do you want to track/control about 1 NPC? You are wanting to create different types of NPCs with some properties, so start off by learning about enumerators. Enumerators are predefined integer values, much like what you were trying with #define, except that you are giving C++ some tools to work with (This pleases the purists that don't like #define, btw.) You also get Intellisense improvement when using enums, and strong type checking loves enumerated types.:



Now, there are only 2 legal, and 1 illegal value defined for NPC_TYPE. As you add types, start here by defining them in this enumerator. Okay, now you can use NPC_TYPE like any integer value.



Now, you can have an array of those, like this:
NPC npcs[100];
npcs[0].type = NPC_MONSTER;

Or, you can generate them dynamically:
PNPC pnpc = new NPC;
pnpc->type = NPC_MONSTER;

There are some substantial differences in those two methods, I suggest you just limit yourself to fixed arrays while you are still defining the structures, and the enumerations.

Once you have them working well, you can use them to create classes that behave in predefined, but different ways.
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 8th May 2008 17:29
I did mention the function substitution but I really didn't want to get into a major discussion/tutorial on the entire subject. WRT using #define over const int, I was echoing sentiment that I saw posted by any number of "experts" on usenet. Of course, that may not have taken into account your argument though, all things considered, I can't see their being able to counter your argument.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
jinzai
18
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 8th May 2008 17:48
I understood the motivation of your post. As per your usual, it was fine, correct and to the point, imo. (As you may have guessed, I don't mind typing long rants on programming.)

I did not make it clear enough that I agreed with your post and was only trying to show that the reservations expressed have to be placed into a context to determine whether they are well-founded, or not.

In fact, I had wanted to demonstrate the basis of class polymorphism using enumerated types, which is off-topic at this point, but might see some relevance in your work.

These things are not covered by anything else here because they are general software engineering concepts, and not linear shoot'em up code. Unfortunately, everyone in (our) the toolchain assumes you are completely up to speed with everything! I really am tired of the phrase "beyond the scope of this discussion". (I am in Help, after all!)

I came about this knowledge by wallowing around in C/C++ for years on my own. I would not necessarily wish it on my worst enemy, but if I can help someone else with it, then I've made a good investment of my time and effort.
Super Mecha Cow
16
Years of Service
User Offline
Joined: 7th May 2008
Location:
Posted: 8th May 2008 18:09
Lol, thanks guys. It was far more information than I ACTUALLY needed, but now I have a lot more tools to work with later.

I tried const int before this message was posted but it didn't work. Right now I have everything set to declare the variables then set them all immediately after. It's sloppy as hell but it's working for now. I'll fool around with const and #define some more.

And yea I only have one parameter for the first demension of the array FOR NOW. I can't get the first NPC working, so I don't really need to make room for 100 more.

enum? I will deffinetly try to apply that to my code if you say it adds new items dynamically. Otherwise I'd hve to search for an empty spot in my array everytime I add a new NPC from in-game.

I'm using a float array because many of the properties will be a value between 0 and 1. I've changed around my code so that when I need to use an int it'll extract the value from the array and change it to a seperate int variable.

Thank you very much, guys. You've been very helpful!
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 8th May 2008 19:07
Quote: "I came about this knowledge by wallowing around in C/C++ for years on my own. I would not necessarily wish it on my worst enemy, but if I can help someone else with it, then I've made a good investment of my time and effort."


I had to "wallow" for a while myself and it took me quite a bit of wallowing to move myself up to C++. Since programming isn't my regular job, unfortunately, I don't get to immerse myself in it enough to try some new things. And, since no one else at work speaks C and it's derivatives as well as I do, I have no one to hash out things I don't understand. Online forums are somewhat helpful but not really a place to discuss when immediate gratification is needed.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 10th May 2008 03:58
Quote: "You never follow a #define with a semi-colon"
Code Plague of Hair Pulling... I know! I've done that and went NUTS!

Quote: "Online forums are somewhat helpful but not really a place to discuss when immediate gratification is needed"
So True!

Login to post a reply

Server time is: 2024-09-29 19:13:36
Your offset time is: 2024-09-29 19:13:36