如何使用矢量存储形状
how to use vector to store a shape?
我首先创建一个窗口,然后创建一系列矩形并将它们放入向量中,然后问题来了。代码无法构建。但我不知道为什么。是我不能以这种方式使用矢量"矢量矩形;"
#include <vector>
#include<string>
#include "Graph.h" // get access to our graphics library facilities
#include "Simple_window.h"
int main()
{
Graph_lib::Point tl{ 100, 100 };
Simple_window win{ tl, 1000, 800, "Simple Window" };
int x_size = 800;
int y_size = 800;
using namespace std;
vector<Graph_lib::Rectangle> rect;//error
for (int x = 0, y = 0; x < x_size&&y < y_size; x += x_grid, y += y_grid)
{
Graph_lib::Rectangle r(Graph_lib::Point(x, y), x_grid, y_grid);
r.set_fill_color(Graph_lib::Color::red);
rect.push_back(r);
}
for (int i = 0; i < 8; i++)
win.attach(rect[i]);
win.attach(grid);
win.wait_for_button();
return 0;
}
D:\Visual Studio Community 2017\VC\Tools\MSVC\14.11.25503\include\xmemory0(856):错误 C2280:"Graph_lib::矩形::矩形(常量 Graph_lib::矩形 &)":尝试引用已删除的函数
D:VS_practiceChengLu_C13DrillincludeGraph.h(311): note: compiler has generated 'Graph_lib::Rectangle::Rectangle' here
D:Visual Studio Community 2017VCToolsMSVC14.11.25503includevector(958): note: see reference to function template instantiation 'void std::_Default_allocator_traits<_Alloc>::construct<_Ty,const _Ty&>(_Alloc &,_Objty *const ,const _Ty &)' being compiled
with
[
_Alloc=std::allocator<Graph_lib::Rectangle>,
_Ty=Graph_lib::Rectangle,
_Objty=Graph_lib::Rectangle
]
D:Visual Studio Community 2017VCToolsMSVC14.11.25503includevector(958): note: see reference to function template instantiation 'void std::_Default_allocator_traits<_Alloc>::construct<_Ty,const _Ty&>(_Alloc &,_Objty *const ,const _Ty &)' being compiled
with
[
_Alloc=std::allocator<Graph_lib::Rectangle>,
_Ty=Graph_lib::Rectangle,
_Objty=Graph_lib::Rectangle
]
D:Visual Studio Community 2017VCToolsMSVC14.11.25503includevector(981): note: see reference to function template instantiation 'void std::vector<T,std::allocator<_Ty>>::emplace_back<const _Ty&>(const _Ty &)' being compiled
with
[
T=Graph_lib::Rectangle,
_Ty=Graph_lib::Rectangle
]
D:Visual Studio Community 2017VCToolsMSVC14.11.25503includevector(980): note: while compiling class template member function 'void std::vector<T,std::allocator<_Ty>>::push_back(const _Ty &)'
with
[
T=Graph_lib::Rectangle,
_Ty=Graph_lib::Rectangle
]
D:VS_practiceChengLu_C13Drillsrcmain.cpp(32): note: see reference to function template instantiation 'void std::vector<T,std::allocator<_Ty>>::push_back(const _Ty &)' being compiled
with
[
T=Graph_lib::Rectangle,
_Ty=Graph_lib::Rectangle
]
d:vs_practicechenglu_c13drillincludestd_lib_facilities.h(72): note: see reference to class template instantiation 'std::vector<T,std::allocator<_Ty>>' being compiled
with
[
T=Graph_lib::Rectangle,
_Ty=Graph_lib::Rectangle
]
D:VS_practiceChengLu_C13Drillsrcmain.cpp(27): note: see reference to class template instantiation 'Vector<Graph_lib::Rectangle>' being compiled
D:VS_practiceChengLu_C13DrillincludeGraph.h(311): note: 'Graph_lib::Rectangle::Rectangle(const Graph_lib::Rectangle &)': function was implicitly deleted because a base class invokes a deleted or inaccessible function 'Graph_lib::Shape::Shape(const Graph_lib::Shape &)'
D:VS_practiceChengLu_C13DrillincludeGraph.h(232): note: 'Graph_lib::Shape::Shape(const Graph_lib::Shape &)': function was explicitly deleted
生成失败。
Graph_lib::Rectangle
类似乎不可复制,因为它的复制构造函数被删除。这样可以防止您直接在实例上使用push_back()
。您可以使用std:move()
:
rect.push_back(std::move(r));
该类也可能不可移动。在这种情况下,您可以在向量中使用std::unique_ptr
:
vector<std::unique_ptr<Graph_lib::Rectangle>> rect;
这将要求您动态分配对象。如果您的编译器支持 C++14 标准或更高版本,则可以使用 std::make_unique
完成此操作:
for (int x = 0, y = 0; x < x_size&&y < y_size; x += x_grid, y += y_grid)
{
auto r = std::make_unique<Graph_lib::Rectangle>(Graph_lib::Point(x, y), x_grid, y_grid);
r->set_fill_color(Graph_lib::Color::red);
rect.push_back(std::move(r));
}
如果 C++14 不可用,您可以使用 new
分配矩形:
for (int x = 0, y = 0; x < x_size&&y < y_size; x += x_grid, y += y_grid)
{
auto r = std::unique_ptr<Graph_lib::Rectangle>(new Graph_lib::Rectangle(Graph_lib::Point(x, y), x_grid, y_grid));
r->set_fill_color(Graph_lib::Color::red);
rect.push_back(std::move(r));
}
相关文章:
- 使用QProcess执行命令,并将结果存储在QStringList中
- 类型总是使用其大小存储在内存中吗
- 使用无符号字符数组有效存储内存
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 使用 Xcode 将双精度存储在数组C++中
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- 使用矢量将文本文件中的输入存储到 2D 数组中
- 使用 char* 存储数据和产生的错误
- 使用成员函数和存储值定义书籍类
- 在地图中使用自定义删除器存储unique_ptr
- 创建一个数组,以允许用户输入存储并稍后在代码中使用
- 使用类在C++中存储和列出变量/方法是否是一种好的做法
- 如何使用 redis-plus-plus 存储二进制数据,就像我想存储结构一样?@for_stack?
- 我如何在C 中的向量中存储和使用不同的类
- 如何在类的成员中存储函数?(使用函数作为回调)
- 将shared_ptr与通用注册表或共享对象存储一起使用.或不
- 存储可以使用不同派生类型初始化的基类型成员变量时,请避免使用new
- 在谷歌云存储中使用服务帐户获取OAuth令牌时'invalid_grant'错误
- 在注册表中存储和使用 Windows 句柄 (WId)