使用 std::vector 和智能指针



我有MainWindow类,有按钮。当按钮单击时,它会打开新表单,并且该表单需要放入矢量。

我像这样编写代码:

操作控件在向量中添加表单,类标头是向量的结构。将来我需要类 bcz,我想在一瞬间关闭矢量中的所有形式。

#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;
};

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

1>C:Program Files (x86)Microsoft Visual Studio 12.0VCincludexmemory0(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.0VCincludetype_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.0VCincludevector(650) : see reference to class template instantiation 'std::is_empty<_Alloc>' being compiled
1>          with
1>          [
1>              _Alloc=std::allocator<Header>
1>          ]
1>          c:usersuserdesktopclientclientHeader.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.0VCincludexmemory0(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.0VCincludexmemory0(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.0VCincludexmemory0(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.0VCincludexmemory0(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.0VCincludexmemory0(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.0VCincludexmemory0(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.0VCincludexmemory0(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.0VCincludexmemory0(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.0VCincludevector(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.0VCincludevector(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.0VCincludevector(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.0VCincludexmemory0(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.0VCincludexmemory0(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.0VCincludexmemory0(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.0VCincludexmemory0(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.0VCincludexmemory0(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.0VCincludevector(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.0VCincludexmemory0(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.0VCincludexmemory0(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.0VCincludexmemory0(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.0VCincludexmemory0(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.0VCincludevector(469) : see reference to class template instantiation 'std::_Simple_types<Header>' being compiled
1>          C:Program Files (x86)Microsoft Visual Studio 12.0VCincludevector(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.0VCincludexmemory0(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.0VCincludexmemory0(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.0VCincludexmemory0(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.0VCincludevector(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.0VCincludevector(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.0VCincludevector(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.0VCincludevector(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.0VCincludevector(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.0VCincludevector(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.0VCincludevector(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.0VCincludevector(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.0VCincludevector(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.0VCincludevector(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.0VCincludevector(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.0VCincludevector(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.0VCincludevector(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.0VCincludevector(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.0VCincludevector(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.0VCincludevector(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 (^) 中的智能指针,请帮助我,我该如何修复它,我需要有打开表单的向量。

^不是

"智能指针",而是.Net托管引用。它不能放入C++容器中,只能放入 .Net 容器中。

我会用System::Collections::Generic::List替换矢量.

相关内容

  • 没有找到相关文章

最新更新