PDA

Click to See Complete Forum and Search --> : Contructor (initialize multi-array)


MOHH
Apr 4th, 2002, 08:53 PM
hello,

having some difficulty here :( with contructors... What i'de like to do is initialize the multidimensional array with all the morse code values..

i can initialize the array in source file ok, but when i try to initialize the same array in a class it won't work...

I'm farily new to classes and contructors, so any explanation on what could be wrong would be great!!


//#include <string.h>
#include <stdlib.h>

#define MORSE = 6;
#define ALPHA = 37;

class morse
{
private:
/*char morseCode[][MORSE] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",
".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",
".--","-..-","-.--","--..",".----","..---","...--","....-",".....",
"-....","--...","---..","----.","-----"};
*/
char morseCode[][MORSE];
char alpha[37];
public:
// char getCode(char alpha);
morse();
};
morse::morse()
{
morseCode[][MORSE] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",
".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",
".--","-..-","-.--","--..",".----","..---","...--","....-",".....",
"-....","--...","---..","----.","-----"};
}


thanks in advance for any help!

Zaei
Apr 4th, 2002, 09:39 PM
That style of initialization is illegal in classes. You are going to have to know the number of morse codes initially, and then copy them in manually(as far as I know), in the constructor.

Z.

MOHH
Apr 4th, 2002, 10:41 PM
thank you,

CornedBee
Apr 6th, 2002, 04:49 PM
The main problem lies in C, not C++: The compiler doesn't know initially how large that array will be, so he can't create the compund type (class, struct or union).
Any class/struct/union with a [] array is invalid in C/C++