gpt4 book ai didi

visual-studio-2008 - 简单:如何在 C++ 中初始化一个新的 unordered_map*(指针)?

转载 作者:行者123 更新时间:2023-12-04 14:38:50 27 4
gpt4 key购买 nike

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

我上面有这段代码可以从指针进行初始化,我可以将指针 ( * ) 排除在外,我可以直接将值赋给它,但问题是我正在使用 C++/Cli Visual Studio 2008,我无法在类范围内定义变量

因为它抛出这个错误:

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

提前致谢!

最佳答案

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

请记住,完成后需要将其删除。

delete Accounts;

关于visual-studio-2008 - 简单:如何在 C++ 中初始化一个新的 unordered_map<std::string,std::string>*(指针)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9925992/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com