gpt4 book ai didi

c++ - 如何使用 vector 来存储形状?

转载 作者:行者123 更新时间:2023-11-30 05:08:09 24 4
gpt4 key购买 nike

我首先创建一个窗口,然后创建一系列矩形并将它们放入一个 vector 中,然后出现问题。代码无法构建。但我不知道为什么。是我不能以这种方式使用 vector "vector rect;"

#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): error C2280: 'Graph_lib::Rectangle::Rectangle(const Graph_lib::Rectangle &)': 尝试引用已删除的函数

D:\VS_practice\ChengLu_C13Drill\include\Graph.h(311): note: compiler has generated 'Graph_lib::Rectangle::Rectangle' here

D:\Visual Studio Community 2017\VC\Tools\MSVC\14.11.25503\include\vector(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 2017\VC\Tools\MSVC\14.11.25503\include\vector(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 2017\VC\Tools\MSVC\14.11.25503\include\vector(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 2017\VC\Tools\MSVC\14.11.25503\include\vector(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_practice\ChengLu_C13Drill\src\main.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_practice\chenglu_c13drill\include\std_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_practice\ChengLu_C13Drill\src\main.cpp(27): note: see reference to class template instantiation 'Vector<Graph_lib::Rectangle>' being compiled

D:\VS_practice\ChengLu_C13Drill\include\Graph.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_practice\ChengLu_C13Drill\include\Graph.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 中:

vector<std::unique_ptr<Graph_lib::Rectangle>> rect;

这将要求您动态分配您的对象。这可以使用 std::make_unique 来完成如果您的编译器支持 C++14 标准或更高版本:

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

关于c++ - 如何使用 vector 来存储形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46996288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com