【发布时间】:2019-04-15 10:35:06
【问题描述】:
我尝试在另一个类中构建一个类时遇到问题。我是 C++ 新手(来自 Java),我不明白为什么我的头文件不好......
为了简单起见,我正在创建一个允许创建、编辑和保存+加载由墙壁制成的地图的应用程序。我编写了一个 Wall 类,它接收参数以及 MainWindow 上的 QGraphicsScene,以便将 Wall 类链接到它的图形表示。 我将 Qt 用于图形方面。因为我是新手,所以事情往往会在我手中爆炸。 MainWindow 有一个 createWall() 函数,每当 QDialog 发出accepted() 时都会调用该函数,并且它的数据会被收集并发送到该函数。
墙.h
#ifndef WALL_H
#define WALL_H
#include <QtWidgets>
class Wall
{
public:
explicit Wall(float x = 0, float y = 0, float lent = 0, float th = 0, float eps = 0, float sig = 0, QGraphicsScene *scene = nullptr);
~Wall();
float getX() const;
void setX(float value);
float getY() const;
void setY(float value);
float getLent() const;
void setLent(float value);
float getTh() const;
void setTh(float value);
float getEps() const;
void setEps(float value);
float getSig() const;
void setSig(float value);
QGraphicsScene *getScene() const;
void setScene(QGraphicsScene *value);
private:
float x;
float y;
float lent;
float th;
float eps;
float sig;
QGraphicsRectItem rect;
QGraphicsScene *scene;
};
#endif // WALL_H
墙.cpp
#include "wall.h"
Wall::Wall(float x1, float y1, float lent1, float th1, float eps1, float sig1, QGraphicsScene *scene1)
{
this->x = x1;
this->y = y1;
this->lent = lent1;
this->th = th1;
this->eps = eps1;
this->sig = sig1;
this->scene = scene1;
QGraphicsRectItem *rect;
QBrush blackBrush(Qt::black);
QPen blackPen(Qt::black);
blackPen.setWidth(1);
rect = scene->addRect(0,0,10,50,blackPen,blackBrush);
rect->setFlag(QGraphicsItem::ItemIsMovable);
}
...
主窗口.cpp
void MainWindow::createWall(float th, float eps, float sig)
{
std::cout << "Thickness = " << th << "\nPermittivity = " << eps << "\nConductivity = " << sig << "\n";
Wall wall(0,0,50,th,eps,sig,scene);
walls.push_back(wall);
wall_number++;
std::cout << "Wall number = " << wall_number << "\n";
}
wall.h 中有 3 个错误:
/usr/include/c++/7/ext/new_allocator.h:136: error: use of deleted function ‘Wall::Wall(const Wall&)’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/.../wall.h:6: error: ‘QGraphicsRectItem::QGraphicsRectItem(const QGraphicsRectItem&)’ is private within this context
/home/.../wall.h:6: error: use of deleted function ‘QGraphicsRectItem::QGraphicsRectItem(const QGraphicsRectItem&)’
class Wall
^~~~
也许我在这里做了一些非常愚蠢的事情......我已经搜索过,似乎我的构造函数根本不起作用,因为它默认被删除。但是我认为我已经正确初始化了它,不是吗?
【问题讨论】:
-
问题出在编译器生成的复制构造函数中,因为
QGraphicsRectItem是不可复制的。 -
欢迎来到 Stackoverflow。要获得更好的答案,请查看stackoverflow.com/help/mcve。我对未来调试类似问题的提示:将您的代码放在版本控制系统中并回滚,直到您的代码再次工作。然后区分提示的变化。
标签: c++ qt constructor