我首先创建一个窗口,然后创建一系列矩形并将它们放入向量中,然后出现问题。代码无法构建。但我不知道为什么。是不是我不能以这种方式使用矢量"矢量矩形;"
List<Album> albums = new ArrayList<Album>();
String artistName = "", albumName = "";
ArrayList<String> albumTracks;
while (inFile.hasNextLine()) {
Scanner inLine = new Scanner(inFile.nextLine());
if(inLine.hasNext()) artistName = inLine.next();
if(inLine.hasNext()) albumName = inLine.next();
albumTracks = new ArrayList<>();
while (inLine.hasNext()) {
albumTracks.add(inLine.next());
}
albums.add(new Album(artistName, albumName, albumTracks));
}
D:\ Visual Studio社区2017 \ VC \ Tools \ MSVC \ 14.11.25503 \ include \ xmemory0(856):错误C2280:&#39; Graph_lib :: Rectangle :: Rectangle(const Graph_lib :: Rectangle&amp; )&#39;:尝试引用已删除的函数
#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;
}
构建失败。
答案 0 :(得分:1)
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));
}