使用std :: vector和智能指针

时间:2015-04-12 16:34:53

标签: visual-studio-2010 visual-studio vector c++-cli smart-pointers

我有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;
};

当我在类Action中添加向量时,此代码崩溃了。输出是:

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(^)中的智能指针,请帮助我,我该如何修复它,我需要有打开表单的向量。

1 个答案:

答案 0 :(得分:0)

^不是智能指针&#39;,它是.Net管理参考。它不能放入C ++容器中,只能放入.Net容器中。

我用System::Collections::Generic::List替换了矢量。