2012-03-29 66 views
0

由于Visual Studio 2008(使用Windows窗体应用程序)上的C++/Cli,我使用了无序地图指针,但我无法为其指定值,抛出一个错误,因为我的代码示例展示给在创建实例后将值指定给unordered_map指针

//... 
    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 
     // 


    } 
    //... 

错误:我希望我的代码是很清晰,所以你可以想像它

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

在此先感谢。

+0

你真的需要'Accounts'是一个指针? – 2012-03-29 13:59:28

+0

我这样做是因为在C++/CLI中,它们不允许你设置它而不用指针。 – Grego 2012-03-29 14:31:57

回答

3

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

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

它抛出一个错误“错误C2059:语法错误:'(''在同一行中,也许错误是在我的问题中显示的它的实例,你认为多数民众赞成吗? – Grego 2012-03-29 13:47:39

+0

我测试了你的新答案,我得到了另一个错误:“错误C2107:非法索引,不允许间接” – Grego 2012-03-29 13:51:56

+0

@格雷戈,对不起。更新。 – hmjd 2012-03-29 13:52:29