使用 std::vector 和智能指针

Using std::vector with smart pointers

我有 MainWindow class,其中有按钮。 当按钮点击时,它会打开一个新的表格,这个表格需要放在向量中。

我的代码是这样的:

Class Action controls adding form in vector and class Header is a structure of vector.I need class bcz in future 我想关闭vector中的所有表单一会。

#pragma once
#include "MyForm.h"
#include <vector>

using namespace std;

ref class Header
{
    public:
    Client::MyForm ^x;
    void Close(){ x->Close();}
};

class Action
{
public:
    vector<Header> vec;
};

当我在 class 操作中添加矢量时,此代码崩溃。输出为:

1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(516): error C3699: '*' : cannot use this indirection on type 'Header'
1>          compiler replacing '*' with '^' to continue parsing
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\type_traits(572) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=Header
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(650) : see reference to class template instantiation 'std::is_empty<_Alloc>' being compiled
1>          with
1>          [
1>              _Alloc=std::allocator<Header>
1>          ]
1>          c:\users\user\desktop\client\client\Header.h(17) : see reference to class template instantiation 'std::vector<Header,std::allocator<_Ty>>' being compiled
1>          with
1>          [
1>              _Ty=Header
1>          ]
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(517): error C3699: '*' : cannot use this indirection on type 'const Header'
1>          compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(521): error C3699: '&' : cannot use this indirection on type 'Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(522): error C3699: '&' : cannot use this indirection on type 'const Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(586): error C3699: '*' : cannot use this indirection on type 'Header'
1>          compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(591): error C3699: '*' : cannot use this indirection on type 'Header'
1>          compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(591): error C3699: '&' : cannot use this indirection on type 'const Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(683): error C3699: '*' : cannot use this indirection on type 'Header'
1>          compiler replacing '*' with '^' to continue parsing
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(755) : see reference to class template instantiation 'std::allocator_traits<_Alloc>' being compiled
1>          with
1>          [
1>              _Alloc=std::allocator<Header>
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(443) : see reference to class template instantiation 'std::_Wrap_alloc<std::allocator<_Ty>>' being compiled
1>          with
1>          [
1>              _Ty=Header
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(579) : see reference to class template instantiation 'std::_Vec_base_types<_Ty,_Alloc>' being compiled
1>          with
1>          [
1>              _Ty=Header
1>  ,            _Alloc=std::allocator<Header>
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(652) : see reference to class template instantiation 'std::_Vector_alloc<false,std::_Vec_base_types<_Ty,_Alloc>>' being compiled
1>          with
1>          [
1>              _Ty=Header
1>  ,            _Alloc=std::allocator<Header>
1>          ]
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(684): error C3699: '*' : cannot use this indirection on type 'const Header'
1>          compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(763): error C3699: '&' : cannot use this indirection on type 'Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(765): error C3699: '&' : cannot use this indirection on type 'const Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(861): error C3699: '*' : cannot use this indirection on type 'Header'
1>          compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(105): error C3699: '*' : cannot use this indirection on type 'Header'
1>          compiler replacing '*' with '^' to continue parsing
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(449) : see reference to class template instantiation 'std::_Is_simple_alloc<std::_Wrap_alloc<std::allocator<_Ty>>>' being compiled
1>          with
1>          [
1>              _Ty=Header
1>          ]
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(107): error C3699: '*' : cannot use this indirection on type 'const Header'
1>          compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(109): error C3699: '&' : cannot use this indirection on type 'Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(111): error C3699: '&' : cannot use this indirection on type 'const Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(122): error C3699: '*' : cannot use this indirection on type 'Header'
1>          compiler replacing '*' with '^' to continue parsing
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(469) : see reference to class template instantiation 'std::_Simple_types<Header>' being compiled
1>          C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(580) : see reference to class template instantiation 'std::_Vector_val<std::_Simple_types<Header>>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(123): error C3699: '*' : cannot use this indirection on type 'const Header'
1>          compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(124): error C3699: '&' : cannot use this indirection on type 'Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0(125): error C3699: '&' : cannot use this indirection on type 'const Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(487): error C3265: cannot declare a managed '_Myfirst' in an unmanaged 'std::_Vector_val<std::_Simple_types<Header>>'
1>          may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(488): error C3265: cannot declare a managed '_Mylast' in an unmanaged 'std::_Vector_val<std::_Simple_types<Header>>'
1>          may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(489): error C3265: cannot declare a managed '_Myend' in an unmanaged 'std::_Vector_val<std::_Simple_types<Header>>'
1>          may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(704): error C3699: '&' : cannot use this indirection on type 'const Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(710): error C3699: '&' : cannot use this indirection on type 'const Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(797): error C3699: '*' : cannot use this indirection on type 'const Header'
1>          compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(864): error C3699: '&&' : cannot use this indirection on type 'Header'
1>          compiler replacing '&&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(887): error C3699: '&&' : cannot use this indirection on type 'Header'
1>          compiler replacing '&&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1118): error C3699: '&' : cannot use this indirection on type 'const Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1242): error C3699: '&' : cannot use this indirection on type 'const Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1320): error C3699: '&' : cannot use this indirection on type 'const Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1326): error C3699: '&' : cannot use this indirection on type 'const Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1332): error C3699: '&' : cannot use this indirection on type 'const Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1581): error C3699: '*' : cannot use this indirection on type 'const Header'
1>          compiler replacing '*' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1652): error C3699: '&' : cannot use this indirection on type 'const Header'
1>          compiler replacing '&' with '^' to continue parsing
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\vector(1745): error C3699: '*' : cannot use this indirection on type 'const Header'
1>          compiler replacing '*' with '^' to continue parsing
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我认为 std::vector 不支持像 VS 中那样的智能指针 (^) 请帮助我,我该如何修复它,我需要打开表单的向量。

^ 不是 'smart pointer',它是 .Net 托管参考。它不能放入 C++ 容器中,只能放入 .Net 容器中。

我会将矢量替换为 System::Collections::Generic::List