Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » need help with declaring a 2d int array

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
need help with declaring a 2d int array
Rash
Member #2,374
May 2002
avatar

X-G said:

EDIT: For the record, Rash's post said "... I ever saw in Allegro ..." before he edited it.

You must be imagining things, because I did no such thing.

Ten to one this thread will now degenerate into a C vs C++ vs C99 discussion...Oh wait! ::)

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

All it means that you shouldn't use macros for constants and inline methods. It's as easy as that.

Right. But that's what most people use them for, like it or not. Like, you know, above. :) Remember this post? Teach the acolyte that macros are evil. When he is a wizard, he can use the black magic when necessary. ;)

William Labbett: MACROS AER TEH EVAL!!1 RUNRUNRUNRUN ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

spellcaster
Member #1,493
September 2001
avatar

Quote:

Teach the acolyte that macros are evil.

That's not needed. Teach everybody that he should use consts and enums for constant values, and inline functions for inline functions.

That's it. No black magic involved here.

EDIT:
Rash: I either want your macro less way for the normal uses of # and ## inside macros, or an apology...
On the other hand, no apology and no macro less way would prove my point as well :)

--
There are no stupid questions, but there are a lot of inquisitive idiots.

Jonatan Hedborg
Member #4,886
July 2004
avatar

Here's a great opportunity to mold a young mind... How would an inlined template-using MIN look? (i have no idea how templates work, but im sure it can be useful :))

X-G
Member #856
December 2000
avatar

I assume something like:

inline template <typename T> T min(T a, T b) { return (a < b ? a : b); }

EDIT: Forgot return type. :P

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

23yrold3yrold
Member #1,134
March 2001
avatar

Off the top of my head ...

   template<class T>
   inline T cadd(T x, T y)
   {
     return (x < y ? x : y);
   }

I might have goofed; it's untested ...

EDIT: All fixed.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Rash
Member #2,374
May 2002
avatar

Ok, to stay in character I'll make my 1000th post useful:

#include <algorithm>
std::min(a,b);

[EDIT]
No need to reinvent the wheel, is there?
[/EDIT]

Jonatan Hedborg
Member #4,886
July 2004
avatar

X-G: Your code didnt compile.
23: your code worked after i replaced X with z1 and Y with z2 ;)

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

No need to reinvent the wheel, is there?

No need to include a standard header for four lines, either. ;) He did ask to see what it would look like ...

Quote:

23: your code worked after i replaced X with z1 and Y with z2

Whoops; right. ;D That's what I get for copying off of Google. ::) All fixed.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

X-G
Member #856
December 2000
avatar

Quote:

X-G: Your code didnt compile.

How about telling me WHY? :P Mine is identical to 23's, apart from the placement of "inline" and line breaks/parentheses that shouldn't matter...

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

23yrold3yrold
Member #1,134
March 2001
avatar

I think it's the inline placement, X-G ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Jonatan Hedborg
Member #4,886
July 2004
avatar

I dont know WHY, but im sure this can help :)

1g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/include/c++" -I"C:/Dev-Cpp/include/c++/mingw32" -I"C:/Dev-Cpp/include/c++/backward" -I"C:/Dev-Cpp/include"
2 
3main.cpp:5: error: syntax error before `<' token
4main.cpp:8: error: definition provided for explicit instantiation
5main.cpp:8: error: non-template used as template
6main.cpp:8: error: syntax error before `{' token
7main.cpp:12: error: ISO C++ forbids declaration of `system' with no type
8main.cpp:12: error: `int std::system' redeclared as different kind of symbol
9C:/Dev-Cpp/include/stdlib.h:362: error: previous declaration of `int
10 system(const char*)'
11main.cpp:12: error: invalid conversion from `const char*' to `int'
12
13main.cpp:13: error: syntax error before `return'
14 
15C:/Dev-Cpp/include/c++/3.3.1/iostream:77: error: definition provided for
16 explicit instantiation
17C:/Dev-Cpp/include/c++/3.3.1/iostream:77: error: non-template used as template
18C:/Dev-Cpp/include/c++/3.3.1/iostream:77: confused by earlier errors, bailing out

spellcaster
Member #1,493
September 2001
avatar

Rash: I think my point is proven then, eh?

--
There are no stupid questions, but there are a lot of inquisitive idiots.

X-G
Member #856
December 2000
avatar

Quote:

main.cpp:5: error: syntax error before `<' token

Err, excuse me? That's where "template" is at. Well, considering how vague errors can be some times, let's just assume it's the placement of the word "inline". :P

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Jonatan Hedborg
Member #4,886
July 2004
avatar

yeah, worked when i moved it.. (template <typename T> inline T min(T a, T b) { return (a < b ? a : b); })

Rash
Member #2,374
May 2002
avatar

Make the return value a reference and you'll have nailed it.

spellcaster: http://www.parashift.com/c++-faq-lite/big-picture.html#faq-6.14

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

Make the return value a reference and you'll have nailed it.

Return a reference? I can see the parameters as references, but by constructing the object right on the return statement ... well, I didn't know you could beat that. I might be wrong, but what are we returning a reference to?

BTW, that is the coolest new entry into the C++ FAQ Lite ever. 8-)

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Rash
Member #2,374
May 2002
avatar

Well in that case make the parameters references as well. The declaration of std::min, after all, is:

template <class T> const T& min(const T& a, const T& b);

spellcaster
Member #1,493
September 2001
avatar

rash: what's your point?
Please read your own post. Then my reply. :)
Then think.

You even quoted me saying that there are uses for macros. Then you replied that this "pisses you off".
I guess this means that you either have a better way to handle these situations, or that you either impolite and/or challenged by the concept of independent thought, and thus need to repeat the old dogmas ("Macros are evil, sha-la-la-la").

While I can understand the latter, you've still been rude :)

--
There are no stupid questions, but there are a lot of inquisitive idiots.

Kitty Cat
Member #2,815
October 2002
avatar

Wouldn't it be hard to create an inline min() that handles floats, doubles, chars, and ints, without using C++? That's one of the problems I had with my game, I had inline min()/max() functions, but they were messing up because it took ints, but I was passing floats (though I wasn't initially aware of this). I changed them to defines like Allegro has and now it works hunky-dory.

Though I do kinda wish the inline functions worked.. branchless min/max is a good thing to have.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

ReyBrujo
Moderator
January 2001
avatar

KC, you could send a third parameter stating the type of the parameters you are sending. As for creating one, this is how DJGPP defines it:

#define min(X, Y) \
    ({ typeof (X) __x = (X), __y = (Y); (__x < __y) ? __x : __y }) 

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Johan Halmén
Member #1,550
September 2001

You know you use too much macros, when you demand syntax hilightning in them.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

 1   2   3 


Go to: