在创建它的实例后将值分配给unordered_map指针

时间:2012-03-29 13:40:36

标签: c++ visual-studio-2008 c++-cli

由于Visual Studio 2008上的C ++ / Cli(使用Windows窗体应用程序),我正在使用无序的地图指针,但我无法为其分配值,因为我的代码示例显示< / p>

 //...
    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"; // The Error is in this line, this is the line 37
        cout << this->Accounts["hello"];
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //


    }
    //...

给出错误:

  

错误4错误C2107:非法索引,不允许间接   C:\ Projects \ Test \ Login.h 37

我希望我的代码足够清晰,以便您可以将其可视化。

提前致谢。

1 个答案:

答案 0 :(得分:3)

Accounts是一个指针,你需要取消引用它:

(*this->Accounts)["hello"] = "test";
cout << (*this->Accounts)["hello"];