2012-03-29 57 views
1
unordered_map<std::string, std::string>* Accounts; 

我有這個代碼在那裏從一個指針初始化,我可以只留下它的指針(*),我可以直接將值分配給它,但問題是,我使用C++ /在Visual Studio 2008中CLI和我不能在那裏類範圍簡單:如何在C++中初始化一個新的unordered_map <std :: string,std :: string> *(指針)?

定義一個變量,因爲它拋出這個錯誤:

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

所以,有人告訴我,我應該做一個指針,然後在初始化構造函數,但是如何從指針創建它? (我認爲像Accounts = new unordered_map) 我總是直接使用它。

我希望我已經夠清楚了。

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

     } 

它拋出這個錯誤:

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

提前感謝!

+0

'Accounts = new unordered_map ;'沒有工作或者什麼? – Shahbaz 2012-03-29 13:10:20

+0

'新的std :: unordered_map ()'? – hmjd 2012-03-29 13:10:35

+0

我把一個@edit,並試圖設置一個值時,發生錯誤。 – Grego 2012-03-29 13:25:13

回答

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

只要記住你需要刪除它,當你完成。

delete Accounts; 
+0

而且您需要使您的類不可複製或添加適當的複製語義(並在C++ 11中移動)。 – 2012-03-29 13:15:46

+0

我把一個@edit,並試圖設置一個值時,它發生了錯誤。 – Grego 2012-03-29 13:23:55

相關問題