class with a static std::map<char,char> member

closed account (iw0XoG1T)
I want to create a class that has access to a std::map<char,char>. I only want to create this map once, not every time I create the new instance of the class.

Is it possible, and how do I do it?

this is an example of what will not work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    struct Big
    {
        static std::map<char,char> tens_comp;
        Big()
        {
            tens_compliment();
        }
        
        void tens_compliment()
        {
            for(char i = '0', j = '9'; i != ('0' + 10); i++, j--)
                tens_comp[i] = j;
        }
        string get_comp(string s)
        {
            string temp;
            for(sit it = s.begin(); it != s.end(); it++)
                temp += tens_comp[*it];
            return temp;
        }
    };


edit: added some context
Last edited on
1
2
3
4
class A{
   static std::map<char, char> m;
};
std::map<char, char> A::m;

edit: in case that doesn't work, what errors did you get?, does it work with some other type?
Last edited on
closed account (iw0XoG1T)
1
2
3
4
5
6
7
8
9
int main(int argc, const char** argv)
try
{
    string test = "1234567890";
    gms::Big test_big;

    cout << test_big.get_comp(test) << '\n';
	return 0;
}



this is the error I received:
g++ obj/bignum.o obj/main.o -o main_test
obj/main.o: In function `gms::Big::tens_compliment()':
/home/gms/Desktop/cplusplus_work/todays_work/bignum.h:25: undefined reference to `gms::Big::tens_comp'
obj/main.o: In function `gms::Big::get_comp(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
/home/gms/Desktop/cplusplus_work/todays_work/bignum.h:31: undefined reference to `gms::Big::tens_comp'
collect2: ld returned 1 exit status
make: *** [main_test] Error 1


note line 4 of my code. static members have to be declared both inside and outside the class.
closed account (iw0XoG1T)
thank you.
Topic archived. No new replies allowed.