Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple: How to initialize a new unordered_map<std::string,std::string>* (pointer) in C++?

unordered_map<std::string, std::string>* Accounts;

I have this code up there to initialize from a pointer, I could just leave the pointer( * ) out of it and I could directly assign the value into it, but the problem is that I'm using C++/Cli on Visual Studio 2008 and I can't define a variable there in the class scope

because it throws this error:

error C4368: cannot define 'Accounts' as a member of managed 'Test::Login': mixed types are not supported C:\ Projects\Test\Login.h 32

So I was told that I should make a pointer and then initialize it in the constructor, but how do I create it from the pointer ? (I thought something like Accounts = new unordered_map) I use to always go directly.

I hope I was clear enough.

@edit

public ref class Login: public System::Windows::Forms::Form
    {
    public:

        unordered_map< std::string, std::string >* Accounts;

        Test(void)
        {
            this->Accounts = new unordered_map<std::string, std::string>();
        this->Accounts["hello"] = "test";
                    cout << this->Accounts["hello"];
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //

        }

it throws this error:

Error 4 error C2107: illegal index, indirection not allowed C:\Projects\Test Login.h 37

Thanks in advance!

like image 841
Grego Avatar asked Jul 11 '26 16:07

Grego


1 Answers

unordered_map<std::string, std::string>* Accounts = new unordered_map<std::string, std::string>();

Just remember you need to delete it when you are done.

delete Accounts;
like image 121
rerun Avatar answered Jul 14 '26 11:07

rerun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!