define array

M

marceliogp

I have a file.h that have all my definitions and I need to define a
array to use in my software, but I don´t know how can I do it.

My define´s that cause error:

....
#define ERROR_SAMPLE1 123
#define ERROR_SAMPLE2 124
....
#define FUNCTIONS_EMP { 20, 15, 22, 53, 34)
....

I´d like to use FUNCTIONS_EMP in my software like a ARRAY, sample:

....
Log( ERROR_SAMPLE1 ); ==> same Log( 123 );
....
Log(FUNCTIONS_EMP[1]; ==> same Log( 15 );
....

Thanks for help

===========================================
Protuguês - Brasil

Eu tenho um arquivo file.h que contém todas as minhas definições e
preciso definir um array para usar no meu software, mas não estou
sabendo como fazer isto.

Minha definição que causou erro foi:

....
#define ERROR_SAMPLE1 123
#define ERROR_SAMPLE2 124
....
#define FUNCTIONS_EMP { 20, 15, 22, 53, 34)
....

Eu gostaria de usar FUNCTIONS_EMP no meu software como um ARRAY,
exemplo:

....
Log( ERROR_SAMPLE1 ); ==> same Log( 123 );
....
Log(FUNCTIONS_EMP[1]; ==> same Log( 15 );
....

Obrigado pela ajuda.
 
H

Howard

I have a file.h that have all my definitions and I need to define a
array to use in my software, but I don4t know how can I do it.

My define4s that cause error:

...
#define ERROR_SAMPLE1 123
#define ERROR_SAMPLE2 124
...
#define FUNCTIONS_EMP { 20, 15, 22, 53, 34)
...

I4d like to use FUNCTIONS_EMP in my software like a ARRAY, sample:

...
Log( ERROR_SAMPLE1 ); ==> same Log( 123 );
...
Log(FUNCTIONS_EMP[1]; ==> same Log( 15 );
...

Thanks for help

Instead of using #define for the array, you could define a global const
array in an implementation file, and declare it as extern in the header file
that other implementation files include.

-Howard
 
R

Robbie Hatley

cleaning-up some apparent typos):

#define FUNCTIONS_EMP {20, 15, 22, 53, 34}
Log(FUNCTIONS_EMP[1]); // same as Log( 15 );

No, that's not the same thing at all! Let's look at the pre-processed
version of what you wrote (ie, what the compiler sees):

Log({20, 15, 22, 53, 34}[1]); // syntax error

Remember, now, the C preprocessor is just a text editor. It's a
rather good text editor in my opinion. I love it and use it a lot.
But none-the-less, it's just a text editor. It doesn't understand
C++ or any other language. It just processes text. You could use
it to process a novel, or a grocery list. I won't parse C++
statements, extract your meaning, and insert the correct syntax for
you. You have to do that yourself.

So, if you want to define an array, you'll have to do it directly
in C++, like so:
void foo()
{
static const int FUNCTIONS_EMP[5] = {20, 15, 22, 53, 34};
// ...
}

Now THAT will work.

(However, if FUNCTIONS_EMP will ever change, use std::vector
instead. In fact, std::vector is better than built-in arrays
for most array applications, because of bounds checking,
expandibility, member functions, etc. I personally only use
built-in arrays for very small, fixed sets of constant values.)


--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top