Mat*_*hew 2 c++ vector abstract c++11
我在调试std::vector<std::unique_ptr<>>抽象类错误时遇到问题.我所知道的错误是试图vector.push_back()在a上使用std::unique_ptr.
Components.h
namespace cr {
class Component {
public:
Component();
virtual ~Component();
virtual void draw() = 0;
virtual void inflate(Frame frame) = 0;
private:
Frame _frame;
};
class ButtonComponent : public Component {
public:
void draw();
void inflate(Frame frame);
};
}
Run Code Online (Sandbox Code Playgroud)
Components.cpp
void cr::ButtonComponent::draw()
{
}
void cr::ButtonComponent::inflate(cr::Frame frame)
{
}
cr::Component::Component()
{
}
cr::Component::~Component()
{
}
Run Code Online (Sandbox Code Playgroud)
Window.h 窗口的一部分
class View {
friend class ViewController;
public:
void render();
void insert(std::unique_ptr<Component> component);
protected:
void inflate();
private:
Frame frame;
std::vector<std::unique_ptr<Component>> _components;
};
Run Code Online (Sandbox Code Playgroud)
Window.cpp 窗口的一部分
void cr::View::render()
{
for (auto& c : _components) {
c->draw();
}
}
void cr::View::insert(std::unique_ptr<Component> component)
{
_components.push_back(component);
}
void cr::View::inflate() {
for (auto& c : _components) {
c->inflate(frame);
}
}
Run Code Online (Sandbox Code Playgroud)
错误输出
1>------ Build started: Project: test, Configuration: Debug Win32 ------
1> RWindow.cpp
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(737): error C2280: 'std::unique_ptr<cr::Component,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
1> with
1> [
1> _Ty=cr::Component
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\memory(1435): note: see declaration of 'std::unique_ptr<cr::Component,std::default_delete<_Ty>>::unique_ptr'
1> with
1> [
1> _Ty=cr::Component
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(857): note: see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,std::unique_ptr<cr::Component,std::default_delete<cr::Component>>&>(_Objty *,std::unique_ptr<cr::Component,std::default_delete<cr::Component>> &)' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<cr::Component,std::default_delete<cr::Component>>,
1> _Objty=std::unique_ptr<cr::Component,std::default_delete<cr::Component>>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(857): note: see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,std::unique_ptr<cr::Component,std::default_delete<cr::Component>>&>(_Objty *,std::unique_ptr<cr::Component,std::default_delete<cr::Component>> &)' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<cr::Component,std::default_delete<cr::Component>>,
1> _Objty=std::unique_ptr<cr::Component,std::default_delete<cr::Component>>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(996): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,std::unique_ptr<cr::Component,std::default_delete<cr::Component>>&>(std::allocator<_Ty> &,_Objty *,std::unique_ptr<cr::Component,std::default_delete<cr::Component>> &)' being compiled
1> with
1> [
1> _Alloc=std::allocator<std::unique_ptr<cr::Component,std::default_delete<cr::Component>>>,
1> _Ty=std::unique_ptr<cr::Component,std::default_delete<cr::Component>>,
1> _Objty=std::unique_ptr<cr::Component,std::default_delete<cr::Component>>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\xmemory0(995): note: see reference to function template instantiation 'void std::allocator_traits<_Alloc>::construct<_Ty,std::unique_ptr<cr::Component,std::default_delete<cr::Component>>&>(std::allocator<_Ty> &,_Objty *,std::unique_ptr<cr::Component,std::default_delete<cr::Component>> &)' being compiled
1> with
1> [
1> _Alloc=std::allocator<std::unique_ptr<cr::Component,std::default_delete<cr::Component>>>,
1> _Ty=std::unique_ptr<cr::Component,std::default_delete<cr::Component>>,
1> _Objty=std::unique_ptr<cr::Component,std::default_delete<cr::Component>>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(1284): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Ty>>::construct<_Ty,std::unique_ptr<cr::Component,std::default_delete<cr::Component>>&>(_Ty *,std::unique_ptr<cr::Component,std::default_delete<cr::Component>> &)' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<cr::Component,std::default_delete<cr::Component>>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(1283): note: see reference to function template instantiation 'void std::_Wrap_alloc<std::allocator<_Ty>>::construct<_Ty,std::unique_ptr<cr::Component,std::default_delete<cr::Component>>&>(_Ty *,std::unique_ptr<cr::Component,std::default_delete<cr::Component>> &)' being compiled
1> with
1> [
1> _Ty=std::unique_ptr<cr::Component,std::default_delete<cr::Component>>
1> ]
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(1276): note: while compiling class template member function 'void std::vector<std::unique_ptr<cr::Component,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>::push_back(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)'
1> with
1> [
1> _Ty=cr::Component
1> ]
1> c:\users\matt\documents\game development\projects\crengine\test\rwindow.cpp(14): note: see reference to function template instantiation 'void std::vector<std::unique_ptr<cr::Component,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>::push_back(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' being compiled
1> with
1> [
1> _Ty=cr::Component
1> ]
1> c:\users\matt\documents\game development\projects\crengine\test\rwindow.h(19): note: see reference to class template instantiation 'std::vector<std::unique_ptr<cr::Component,std::default_delete<_Ty>>,std::allocator<std::unique_ptr<_Ty,std::default_delete<_Ty>>>>' being compiled
1> with
1> [
1> _Ty=cr::Component
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Run Code Online (Sandbox Code Playgroud)
项目背景
一个简单的GUI,它有一个窗口,一个视图控制器,可以保存多个视图.视图包含多个组件(也称为小部件).在项目期间使用SFML和OpenGL.
下次请将代码简化为相关部分,其中包含错误消息.其余的只是噪音.
#include <memory>
#include <vector>
int main()
{
std::vector<std::unique_ptr<int>> v;
std::unique_ptr<int> p;
v.push_back(p);
}
Run Code Online (Sandbox Code Playgroud)
这会尝试将一个副本插入p到向量中,这是不允许的,因为您无法复制unique_ptr(它是唯一的).
您需要移动它而不是复制它,以便所有权从p向量转移到向量中的新元素:
v.push_back(std::move(p));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
576 次 |
| 最近记录: |