-1

I have this code:

#include <iostream>
#include <string>
#include <map>

namespace Test
{
    class Storage
    {
        public:

        static std::map<std::string, std::string> storageMemory;

        static void Set(std::string name, std::string value)
        {
            if (name.length() == 0 && value.length() == 0) {
                return;
            }

            storageMemory[name] = value;
        }

        static std::string Get(std::string name)
        {
            return storageMemory[name];
        }

    };
}

My idea:

Test::Storage::Set("key", "value"); // to set value
Test::Storage::Get("key"); // to get value by key

Have any ideas?

Why this construction not works?

For example when I create this logic in PHP this working fine.

Help me please!

Thanks!

Sergei Dub
  • 93
  • 1
  • 4
  • *How* it doesn't work? – Yksisarvinen Mar 27 '20 at 13:31
  • Why do you think that it does not work? – eerorika Mar 27 '20 at 13:31
  • 1
    You need to define static variables outside of the class – SuperStormer Mar 27 '20 at 13:32
  • 2
    Please explain what "*construction not works*" means and provide a complete [repro] for the issue, including any error messages you receive. Something working in PHP is not a good indicator that it will work in C++. The languages are completely different. C++ really needs to be learned in a structured manner, e.g. using one of the [recommended books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – walnut Mar 27 '20 at 13:32
  • Looks OK to me, except that it is incomplete. Please don't just say `it's not working`, say what isn't working. If you want some help (which I think you do) then you need to help people help you. – john Mar 27 '20 at 13:33
  • Does this answer your question? [Initialize static variables in C++ class?](https://stackoverflow.com/questions/5019856/initialize-static-variables-in-c-class) – SuperStormer Mar 27 '20 at 13:34
  • @SergeiDub: Welcome at SO! Please note that without a complete minimal reproducible example (code) we can only guess possible solutions and you will "earn" frustrating down- or "close"-votes so please add at least a minimal main function and the error message you (by editing your question). Also: Improve the question headline (don't use your class name but describe the problem using good keywords) THX :-) – R Yoda Mar 27 '20 at 14:54

1 Answers1

0

Please keep in mind that static mutable members are dangerous and not thread safe. To use a static member, you need to instantiate it out of the class:

#include <iostream>
#include <string>
#include <map>

namespace Test
{
    class Storage
    {
        public:

        static std::map<std::string, std::string> storageMemory;

        static void Set(std::string name, std::string value)
        {
            if (name.length() == 0 && value.length() == 0) {
                return;
            }

            storageMemory[name] = value;
        }

        static std::string Get(std::string name)
        {
            return storageMemory[name];
        }

    };

    std::map<std::string, std::string> Storage::storageMemory = {};
}
Alexandre Senges
  • 1,474
  • 1
  • 13
  • 22
  • 1
    Why the downvote, please explain? IMHO it works (`storageMemory` was not initialized and C++ allows initalization of non-scalar types only outside of the class body...) – R Yoda Mar 27 '20 at 13:59
  • 1
    @RYoda I am not the downvoter, but one problem with this answer is that it answers the question assuming things OP didn't show. The problem OP is experiencing could have other causes. For example OP might have tried to call `Test::Storage::Set("key", "value");` at file scope rather than block scope, which is also a common mistake. Really, the question should be closed until OP provides a [repro] with error message. Furthermore, this answer can be easily misleading, because the definition should not be put in a header file, while the class definitions usually are, but here they are together. – walnut Mar 27 '20 at 14:44
  • THX, fair point, the question is really incomplete (not even showing the error message). I still have mixed feelings about downvoting working answers (even though assumptions are made). – R Yoda Mar 27 '20 at 14:50
  • Thanks for the suggestions, based on the question, I assumed it was something wrong with the `construction` and didn't think about other possible issues. That would be nice if we had some feedbacks from OP. – Alexandre Senges Mar 27 '20 at 15:04