【发布时间】:2018-04-10 07:31:17
【问题描述】:
我首先创建了一个窗口,然后创建了一系列矩形并将它们放入一个向量中,然后问题来了。代码无法构建。但我不知道为什么。是不是我不能用这种方式使用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
构建失败。
【问题讨论】:
-
错误消息是不言自明的,正如 Fred Larson 指出的那样。您需要阅读“Graph_lib”的文档以了解为什么
Rectangle不可复制(即具有已删除的复制构造函数)。 -
那么,如何更正此代码?我的意思是如何打印出一系列矩形?如果向量不起作用。
-
无论如何,非常感谢!
-
另一个问题。我添加“win.attach(r);win.wait_for_button();”在循环中,但矩形只是一一显示,最后什么都没有显示。我不知道为什么。我只是希望所有这些 rect 一起出现。
-
我投票结束这个问题作为题外话,因为问题相当于“为什么不能将此第三方(非标准)不可复制类型存储在标准容器中只能存储可复制的对象?”。 OP 需要阅读第三方库的文档以了解为什么这种类型是不可复制的,并找到替代方案。无法访问第三方库的人无法做到这一点。因此,这个问题在 C++ 论坛中是题外话。