Domanda

I have problem with init array

Here is my code

*L4_Z2_RzymArab.hpp*

class L4_Z2__RzymArab
{
public:
    //zmienne

    //konsturktory i destruktory
    L4_Z2__RzymArab();
    ~L4_Z2__RzymArab();

    //funkcje
    static int rzym2arab(string rzym) throw(RzymArabException);
    static string arab2rzym(int arab) throw(RzymArabException);

    static void set__data_length(int d);

private:
    //zmienne
    static int data_length;
    static string liczby[]; //Rzymskie
    static int liczby_ar[]; //Cyfry

    static char accept_chars[]; //Dozwolone znaki

    //Wielkość tablic
    static int size__liczny;
    static int size__liczby_ar;
    static int size__accept_chars;//sizeof(a)/sizeof(*a)


    //funkcje
    static bool is_roman_num(string rzym);


};


L4_Z2_RzymArab.cpp
//Romain numeric system
string L4_Z2__RzymArab::liczby[] = 
{"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};

//Arabic numeric system
int L4_Z2__RzymArab::liczby_ar[] = 
{1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
//Level 3 - [12, 12]
//Level 2 - [8, 11]
//Level 1 - [4, 7]
//Level 0 - [0, 3]

//Array of allowed chars
char L4_Z2__RzymArab::accept_chars[]  = 
{'I', 'V', 'X', 'L', 'C', 'D', 'M'};

int size__liczny = sizeof(L4_Z2__RzymArab::liczby) / sizeof(L4_Z2__RzymArab::*liczby);
int size__liczby_ar = sizeof(L4_Z2__RzymArab::liczby_ar) / sizeof(L4_Z2__RzymArab::*liczby_ar);
int size__accept_chars = sizeof(L4_Z2__RzymArab::accept_chars) / sizeof(L4_Z2__RzymArab::*accept_chars);

    [...]

http://www.cplusplus.com/doc/tutorial/arrays/

I got errors:

          L4_Z2__RzymArab.cpp:19:9: error: ‘std::string L4_Z2__RzymArab::liczby [13]’ is private
                string L4_Z2__RzymArab::liczby[] = 
                             ^
            L4_Z2__RzymArab.cpp:34:45: error: within this context
                int size__liczny = sizeof(L4_Z2__RzymArab::liczby) / sizeof(L4_Z2__RzymArab::*liczby);
                                                                 ^
            L4_Z2__RzymArab.cpp:34:79: error: expected unqualified-id before ‘*’ token
                int size__liczny = sizeof(L4_Z2__RzymArab::liczby) / sizeof(L4_Z2__RzymArab::*liczby);
                                                                                                   ^
            L4_Z2__RzymArab.cpp:34:80: error: ‘liczby’ was not declared in this scope
                int size__liczny = sizeof(L4_Z2__RzymArab::liczby) / sizeof(L4_Z2__RzymArab::*liczby);
                                                                                                    ^
            L4_Z2__RzymArab.cpp:23:6: error: ‘int L4_Z2__RzymArab::liczby_ar [13]’ is private
                int L4_Z2__RzymArab::liczby_ar[] = 
                        ^
            L4_Z2__RzymArab.cpp:35:48: error: within this context
                int size__liczby_ar = sizeof(L4_Z2__RzymArab::liczby_ar) / sizeof(L4_Z2__RzymArab::*liczby_ar);
                                                                    ^
            L4_Z2__RzymArab.cpp:35:85: error: expected unqualified-id before ‘*’ token
                int size__liczby_ar = sizeof(L4_Z2__RzymArab::liczby_ar) / sizeof(L4_Z2__RzymArab::*liczby_ar);
                                                                                                         ^
            L4_Z2__RzymArab.cpp:35:86: error: ‘liczby_ar’ was not declared in this scope
                int size__liczby_ar = sizeof(L4_Z2__RzymArab::liczby_ar) / sizeof(L4_Z2__RzymArab::*liczby_ar);
                                                                                                          ^
            L4_Z2__RzymArab.cpp:31:7: error: ‘char L4_Z2__RzymArab::accept_chars [7]’ is private
                char L4_Z2__RzymArab::accept_chars[]  = 
                         ^
            L4_Z2__RzymArab.cpp:36:51: error: within this context
                int size__accept_chars = sizeof(L4_Z2__RzymArab::accept_chars) / sizeof(L4_Z2__RzymArab::*accept_chars);    
                                                                       ^
            L4_Z2__RzymArab.cpp:36:91: error: expected unqualified-id before ‘*’ token
                int size__accept_chars = sizeof(L4_Z2__RzymArab::accept_chars) / sizeof(L4_Z2__RzymArab::*accept_chars);    
                                                                                                               ^
            L4_Z2__RzymArab.cpp:36:92: error: ‘accept_chars’ was not declared in this scope
                int size__accept_chars = sizeof(L4_Z2__RzymArab::accept_chars) / sizeof(L4_Z2__RzymArab::*accept_chars); 

Where is my fault? What i should improve?

È stato utile?

Soluzione

You define

int size__liczny = ...
// etc.

when you should define

int L4_Z2__RzymArab::size__liczny = ...
// etc.

By the way, if you have a class containing only static members, you might as well make it a namespace instead, like

namespace L4_Z2__RzymArab
{
    //funkcje
    int rzym2arab(string rzym) throw(RzymArabException);
    // etc... (have other public members here)
}

And

// Unnamed namespace for variables local to this file only
namespace
{
    string liczby[] = { ... };
    // etc... (the previous private members declared and defined here)
}

int L4_Z2__RzymArab::rzym2arab(string rzym)
{
    ...
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top