C++ 如何使用矢量存储形状?

C++ 如何使用矢量存储形状?,c++,vector,graph,lib,C++,Vector,Graph,Lib,我首先创建一个窗口,然后创建一系列矩形并将它们放入一个向量中,然后进入问题。代码无法生成。但我不知道为什么。就是我不能以这种方式使用vector“vector rect #包括 #包括 #包括“Graph.h”//访问我们的图形库设施 #包括“Simple_window.h” int main() { 图_-lib::点tl{100100}; 简单窗口赢{tl,1000800,“简单窗口”}; int x_size=800; int y_尺寸=800; 使用名称空间std; 向量rect;//错

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

#包括
#包括
#包括“Graph.h”//访问我们的图形库设施
#包括“Simple_window.h”
int main()
{
图_-lib::点tl{100100};
简单窗口赢{tl,1000800,“简单窗口”};
int x_size=800;
int y_尺寸=800;
使用名称空间std;
向量rect;//错误
对于(int x=0,y=0;x
D:\Visual Studio Community 2017\VC\Tools\MSVC\14.11.25503\include\xmemory0(856):错误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
D:\VS\u practice\ChengLu\u C13Drill\include\Graph.h(311):注意:编译器已在此处生成“Graph\u lib::Rectangle::Rectangle”
D:\Visual Studio Community 2017\VC\Tools\MSVC\14.11.25503\include\vector(958):注意:请参阅正在编译的函数模板实例化“void std::_Default_allocator_traits::construct(_Alloc&,_Objty*const,const _Ty&)”
具有
[
_Alloc=std::分配器,
_Ty=图形库::矩形,
_对象=图形库::矩形
]
D:\Visual Studio Community 2017\VC\Tools\MSVC\14.11.25503\include\vector(958):注意:请参阅正在编译的函数模板实例化“void std::_Default_allocator_traits::construct(_Alloc&,_Objty*const,const _Ty&)”
具有
[
_Alloc=std::分配器,
_Ty=图形库::矩形,
_对象=图形库::矩形
]
D:\Visual Studio Community 2017\VC\Tools\MSVC\14.11.25503\include\vector(981):注意:请参阅正在编译的函数模板实例化“void std::vector::emplace_back(const _Ty&)”的参考
具有
[
T=图形库::矩形,
_Ty=图形库::矩形
]
D:\Visual Studio Community 2017\VC\Tools\MSVC\14.11.25503\include\vector(980):注意:在编译类模板成员函数“void std::vector::push_back(const _Ty&)”时
具有
[
T=图形库::矩形,
_Ty=图形库::矩形
]
D:\VS\u practice\ChengLu\u C13Drill\src\main.cpp(32):注意:请参阅正在编译的函数模板实例化“void std::vector::push_back(const)&”的参考
具有
[
T=图形库::矩形,
_Ty=图形库::矩形
]
d:\vs\u practice\chenglu\u c13drill\include\std\u lib\u facilities.h(72):注意:请参阅正在编译的类模板实例化“std::vector”的参考
具有
[
T=图形库::矩形,
_Ty=图形库::矩形
]
D:\VS\u practice\ChengLu\u C13Drill\src\main.cpp(27):注意:请参阅正在编译的类模板实例化“Vector”的参考
D:\VS\u practice\ChengLu\u C13Drill\include\Graph.h(311):注意:'Graph\u lib::Rectangle::Rectangle(const Graph\u lib::Rectangle&'):函数被隐式删除,因为基类调用了已删除或无法访问的函数“Graph\u lib::Shape::Shape(const Graph\u lib::Shape&)”
D:\VS\u practice\ChengLu\u C13Drill\include\Graph.h(232):注意:'Graph\u lib::Shape::Shape(const-Graph\u lib::Shape&'):函数被显式删除

生成失败。

似乎
图形库::矩形
类不可复制,因为其复制构造函数已被删除。这会阻止您直接在实例上使用
push_back()
。您可以使用
std:move()

类也可能是不可移动的。在这种情况下,可以在向量中使用:

vector<std::unique_ptr<Graph_lib::Rectangle>> rect;
如果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));
}
for(int x=0,y=0;x设置填充颜色(图形库::颜色::红色);
直线推回(标准::移动(r));
}

正如弗雷德·拉森(Fred Larson)所指出的,错误消息是不言自明的。您需要阅读“Graph_lib”的文档,以了解为什么
矩形
不可复制(即具有已删除的复制构造函数)。那么,如何更正此代码?我的意思是如何打印出一系列的矩形?如果向量不起作用。无论如何,非常感谢!另一个问题。我在循环中添加了“win.attach(r);win.wait_for_button();”,但矩形只是一个接一个地显示,最后什么也没有显示。我不知道为什么。我只是希望所有这些rect一起出现。我投票结束这个问题,因为这个问题相当于“为什么这个第三方(非标准)不可复制类型不能存储在一个只能存储可复制对象的标准容器中?”。OP需要阅读第三方库的文档,以了解此类型不可复制的原因,并找到替代方法。无法访问第三方图书馆的人不能这样做。这样,这个问题在C++ FuMU.OK中是偏离主题的,但是它说“STD没有成员”UNIQUYPTR,“你需要<代码> >包含< /COD>。在第二个,它说“不能从‘GrasoLIB::矩形’*到‘STD::UnQuyPPTR’”它现在应该被固定。它说:错误C2280:'STD::UnQuyJPT::UnQuyQ-PTR(const STD::UnQuyJPt&)“:尝试引用已删除的函数
vector<std::unique_ptr<Graph_lib::Rectangle>> rect;
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));
}
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));
}